home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / RAND / UNSPLIT / BM214SRC.ZIP / 68030 / DOOM.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-06-25  |  71.7 KB  |  2,483 lines

  1.  
  2.     nolist
  3.  
  4. ;*======================================================*
  5. ;*    DSP Doom core: latest update 23/02/96        *
  6. ;*======================================================*
  7. ;*    Perform all intensive & mathematical jobs    *
  8. ;*======================================================*
  9.  
  10. PBC            =    $FFE0
  11. HCR            =    $FFE8
  12. HSR            =    $FFE9
  13. HTX            =    $FFEB
  14.  
  15. ;-------------------------------------------------------*
  16.  
  17. runlink            =    0            ; linked run structure
  18. rundata            =    1
  19. runsize            =    2
  20.  
  21. zmin            =    12            ; viewplane z
  22.  
  23. max_lines        =    200
  24.  
  25. ;-------------------------------------------------------*
  26.  
  27.     include        "i:\projects\bad_mood\68030\include\dspconst.s"
  28.  
  29. ;-------------------------------------------------------*
  30.     org        y:0
  31. ;-------------------------------------------------------*
  32. ;    Temp register for floor mapping function    *
  33. ;-------------------------------------------------------*
  34.  
  35. map_data:
  36. map_xi:            ds    1
  37. map_x:            ds    1
  38. map_yi:            ds    1
  39. map_x1:            ds    1
  40. map_v:            ds    1
  41. map_xo:            ds    1
  42. map_uvs:        ds    1
  43. map_sinauvs:        ds    1
  44. map_cosauvs:        ds    1
  45.  
  46. ;-------------------------------------------------------*
  47.     org        x:0
  48. ;-------------------------------------------------------*
  49. ;    General temp registers                *
  50. ;-------------------------------------------------------*
  51.  
  52. temp1:            ds    1
  53. temp2:            ds    1
  54.  
  55. ;-------------------------------------------------------*
  56.     org        l:2
  57. ;-------------------------------------------------------*
  58. ;    Wall upper / lower edge accumulators        *
  59. ;-------------------------------------------------------*
  60.  
  61. edge_increments:    
  62. y1_increment:        ds    1    ; 24:24
  63. y1_accumulator:        ds    1    ; 24:24
  64. y2_increment:        ds    1    ; 24:24
  65. y2_accumulator:        ds    1    ; 24:24
  66. t_increment:        ds    1    ; 24:24
  67. t_accumulator:        ds    1    ; 24:24
  68.  
  69. ;-------------------------------------------------------*
  70. ;    org        x:8
  71. ;-------------------------------------------------------*
  72. ;    Upper-edge clipping registers            *
  73. ;-------------------------------------------------------*
  74.  
  75. column_thistop:        ds    1
  76. column_lasttop:        ds    1
  77.  
  78. ;-------------------------------------------------------*
  79.     org        l:10
  80. ;-------------------------------------------------------*
  81. ;    Multi-purpose (overlapping) registers        *
  82. ;-------------------------------------------------------*
  83.  
  84. vertices:                        ; vertex array for nodes
  85. vertex1:
  86. ctop_z:            ds    1            ; ceiling top-z
  87. cbot_z:            ds    1            ; ceiling bot-z
  88. vertex2:
  89. ftop_z:            ds    1            ; floor top-z
  90. fbot_z:            ds    1            ; floor bot-z
  91. vertex3:
  92. t:            ds    1            ; t-factor
  93. ti:            ds    1            ; t-factor increment
  94. vertex4:
  95.             ds    1
  96.             ds    1
  97.  
  98. ;-------------------------------------------------------*
  99.     org        x:18
  100. ;-------------------------------------------------------*
  101.  
  102. ;=======================================================*
  103. ;    X buffers                    *
  104. ;=======================================================*
  105.  
  106. ;-------------------------------------------------------*
  107. ;    Floor left-edge generation stack        *
  108. ;-------------------------------------------------------*
  109.  
  110. flatstack:        ds    max_lines
  111.  
  112. line_regs:
  113. line_x1:        ds    1
  114. line_y1:        ds    1
  115. line_x2:        ds    1
  116. line_y2:        ds    1
  117.  
  118. ;-------------------------------------------------------*
  119. ;    Wall upper-edge clipping array            *
  120. ;-------------------------------------------------------*
  121.  
  122. miny:            ds    320+1
  123.  
  124. ;-------------------------------------------------------*
  125. ;    Overlap buffers                    *
  126. ;-------------------------------------------------------*
  127.  
  128. x1list:            ds    320+2            ; defrag list #1
  129. x2list:            ds    320+2            ; defrag list #2
  130. oslist:            ds    320+2            ; defrag list #3
  131.  
  132. ;-------------------------------------------------------*
  133. ;    Occlusion list                    *
  134. ;-------------------------------------------------------*
  135.  
  136. occlusion_list:        ds    320            ; indicates 'sealed' columns
  137.  
  138. ;-------------------------------------------------------*
  139. ;    Remaining space used for run storage        *
  140. ;-------------------------------------------------------*
  141.  
  142. runs:
  143.  
  144. ;-------------------------------------------------------*
  145.     org        y:8
  146. ;-------------------------------------------------------*
  147. ;    Lower-edge clipping registers            *
  148. ;-------------------------------------------------------*
  149.  
  150. column_thisbot:        ds    1
  151. column_lastbot:        ds    1
  152.  
  153. ;-------------------------------------------------------*
  154.     org        y:18
  155. ;-------------------------------------------------------*
  156.     
  157. ;-------------------------------------------------------*
  158. ;    Frequently used constants            *
  159. ;-------------------------------------------------------*
  160.  
  161. x1_msk:            dc    $000FFF            ; x2:x1 run separator mask (x1)
  162. x2_msk:            dc    $FFF000            ; x2:x1 run separator mask (x2)
  163. column_rout:        dc    perspected_column    ; pointer to correct column routine
  164. one:            dc    $000001            ; quick-immediate #>1,??
  165. two:            dc    $000002            ; quick-immediate #>2,??
  166. viewplane:        dc    zmin            ; clipping plane z
  167. pnorm:            dc    1<<((6+7+8+8)-24-1)    ; evaluated constant for mapping function
  168.  
  169. ;-------------------------------------------------------*
  170. ;    Single-cycle shift constants            *
  171. ;-------------------------------------------------------*
  172.  
  173. rshft19:
  174. lshft5:            dc    (1<<(5-1))
  175. rshft18:
  176. lshft6:            dc    (1<<(6-1))
  177. rshft17:
  178. lshft7:            dc    (1<<(7-1))
  179. rshft16:
  180. lshft8:            dc    (1<<(8-1))
  181. rshft14:
  182. lshft10:        dc    (1<<(10-1))
  183. rshft13:
  184. lshft11:        dc    (1<<(11-1))
  185. rshft12:
  186. lshft12:        dc    (1<<(12-1))
  187. rshft9:
  188. lshft15:        dc    (1<<(15-1))
  189. rshft8:
  190. lshft16:        dc    (1<<(16-1))
  191. rshft7:
  192. lshft17:        dc    (1<<(17-1))
  193. rshft6:
  194. lshft18:        dc    (1<<(18-1))
  195. rshft5:
  196. lshft19:        dc    (1<<(19-1))
  197.  
  198. ;-------------------------------------------------------*
  199. ;    Frequently used pointers & addresses        *
  200. ;-------------------------------------------------------*
  201.  
  202. HTX_ptr:        dc    HTX            ; host port
  203. flatstack_ptr:        dc    flatstack        ; run stack
  204. lastslots_ptr:        dc    lastslots        ; linked run last-slot addresses
  205. rundata_ptr:        dc    (runs+rundata)        ; run buffer (runs)
  206. runlink_ptr:        dc    (runs+runlink)        ; run buffer (links)
  207. x1list_ptr:        dc    x1list            ; run x1 merge-list
  208. x2list_ptr:        dc    x2list            ; run x2 merge-list
  209. oslist_ptr:        dc    oslist            ; run offset list
  210. tlist_ptr:        dc    tlist            ; run tracking list
  211.  
  212. ;-------------------------------------------------------*
  213. ;    Frequently used variables            *
  214. ;-------------------------------------------------------*
  215.  
  216. index:            ds    1            ; floating index for linked runs
  217. columns:        ds    1            ; total columns occluded so far
  218. attributes:        ds    1            ; wall attributes
  219.  
  220. window_regs:
  221. width:            ds    1            ; variables describing viewport
  222. hwid:            ds    1
  223. height:            ds    1
  224. hhig:            ds    1
  225. r_height:        ds    1
  226. hs_width:        ds    1
  227. vs_width:        ds    1
  228.  
  229. ceiling_bounds:
  230. ctop:            ds    1            ; ceiling bounding box
  231. cbot:            ds    1
  232.  
  233. floor_bounds:
  234. ftop:            ds    1            ; floor bounding box
  235. fbot:            ds    1
  236.  
  237. noderegs:
  238. node_y2:        ds    1            ; node array
  239. node_y1:        ds    1
  240. node_x1:        ds    1
  241. node_x2:        ds    1
  242.  
  243. node_imin:        ds    1            ; node bounding box
  244. node_imax:        ds    1
  245.  
  246. px:            ds    1            ; player information
  247. py:            ds    1
  248. pangle:            ds    1
  249. sina:            ds    1
  250. cosa:            ds    1
  251.  
  252. map_tnorm:        ds    1            ; normalizing constant for mapping
  253.  
  254. wall_u:            ds    1            ; wall mapping description variables
  255. wall_umag:        dc    128
  256. wall_vmag:        dc    128
  257. wall_usize:        dc    128
  258. wall_vsize:        dc    128
  259. wall_uoff:        ds    1
  260. wall_voff:        ds    1
  261.  
  262. wall_sz:        ds    1            ; wall mapping function variables
  263. wall_fsz:        ds    1
  264. wall_dz:        ds    1
  265. wall_fdz:        ds    1
  266.  
  267. mlum2:            ds    1
  268. mlum3:            ds    1
  269. mlum_limit:        dc    64-1
  270. z:            ds    1
  271.  
  272. ci1:            ds    1            ; wall x-bounds
  273. ci2:            ds    1
  274.  
  275. wall_z1:        ds    1            ; wall z-bounds
  276. wall_z2:        ds    1
  277.  
  278. cy:            ds    1            ; ceiling y-bounds
  279. cj1:            ds    1
  280. cj2:            ds    1
  281. fy:            ds    1            ; floor y-bounds
  282. fj1:            ds    1
  283. fj2:            ds    1
  284.  
  285. run_ptr:        ds    1            ; floating run pointer
  286. ceiling_luminance:    ds    1
  287. floor_luminance:    ds    1
  288.  
  289. hwid_8:            ds    1
  290.  
  291. ;=======================================================*
  292. ;    Y buffers                    *
  293. ;=======================================================*
  294.  
  295. ;-------------------------------------------------------*
  296. ;    Wall lower-edge clipping array            *
  297. ;-------------------------------------------------------*
  298.  
  299. maxy:            ds    320+1
  300.  
  301. ;-------------------------------------------------------*
  302. ;    DSP host commands                *
  303. ;-------------------------------------------------------*
  304.  
  305. commands:        dc    AddMidWall
  306.             dc    AddUpperWall
  307.             dc    AddLowerWall
  308.             dc    ResizeWindow
  309.             dc    NewScene
  310.             dc    NewSsector
  311.             dc    GetSsector
  312.             dc    NodeInCone
  313.             dc    RotateMap
  314.             dc    LinearCalc
  315.             dc    PerspectCalc
  316.             dc    SetLuminance
  317.             dc    ProjectWall
  318.             dc    AddTransWall
  319.  
  320. ;-------------------------------------------------------*
  321. ;    Octant elimination routines            *
  322. ;-------------------------------------------------------*
  323.  
  324. octs:            dc    oct_2
  325.             dc    oct_3
  326.             dc    oct_4
  327.             dc    oct_5
  328.             dc    oct_6
  329.             dc    oct_7
  330.             dc    oct_0
  331.             dc    oct_1
  332.  
  333. ;-------------------------------------------------------*
  334. ;    Synthesized polygon definition for nodes    *
  335. ;-------------------------------------------------------*
  336.  
  337. linedefs:        dc    vertex1,vertex2,vertex3,vertex4,vertex1
  338.  
  339. ;-------------------------------------------------------*
  340.  
  341. tlist:            ds    32            ; defrag tracking list
  342.  
  343. ;-------------------------------------------------------*
  344. end_iy:
  345. ;-------------------------------------------------------*
  346.  
  347. ;-------------------------------------------------------*
  348. ;    DSP / Host communication macros            *
  349. ;-------------------------------------------------------*
  350.  
  351. dspread_s        macro
  352. _rd    jclr        #0,x:<<HSR,_rd
  353.     move        x:(r7),n7
  354.     move        n7,a
  355.     jclr        #15,n7,_sk
  356.     add        b,a
  357. _sk:    
  358.     endm
  359.  
  360. dspread_u        macro    acc
  361. _rd    jclr        #0,x:<<HSR,_rd
  362.     move        x:(r7),n7
  363.     move        n7,acc
  364.     endm
  365.  
  366. dspread            macro    var
  367. _rd    jclr        #0,x:<<HSR,_rd
  368.     movep        x:<<HTX,var
  369.     endm
  370.  
  371. dspwrite        macro    var
  372. _wr    jclr        #1,x:<<HSR,_wr
  373.     movep        var,x:<<HTX
  374.     endm
  375.  
  376. dspwaitread        macro
  377. _rd    jclr        #0,x:<<HSR,_rd
  378.     endm
  379.  
  380. dspwaitwrite        macro
  381. _wr    jclr        #1,x:<<HSR,_wr
  382.     endm
  383.  
  384. ;-------------------------------------------------------*
  385. ;    Wall edge generator                *
  386. ;-------------------------------------------------------*
  387.  
  388. advance_column            macro
  389.     move            l:y1_increment,a
  390.     move            l:y1_accumulator,b
  391.     add        a,b    l:y2_increment,a
  392.     move            b,l:y1_accumulator
  393.     move            l:y2_accumulator,b
  394.     add        a,b    l:t_increment,a
  395.     move            b,l:y2_accumulator
  396.     move            l:t_accumulator,b
  397.     add        a,b    n0,a
  398.     move            y:one,x0
  399.     add        x0,a    b,l:t_accumulator
  400.     move        a,n0
  401.     move        a,n4
  402.     endm
  403.  
  404. ;-------------------------------------------------------*
  405. ;    Wall edge top / bottom clipper            *
  406. ;-------------------------------------------------------*
  407. ;    thisminy    x:(r0+n0)
  408. ;    thismaxy    y:(r4+n4)
  409. ;    lastminy    x:(r0)
  410. ;    lastmaxy    y:(r4)
  411. ;    thistop        x:(r3)
  412. ;    thisbot        y:(r5)
  413. ;    lasttop        x:(r3+n3)
  414. ;    lastbot        y:(r5+n5)
  415. ;-------------------------------------------------------*
  416.  
  417. load_column            macro
  418.     move            #0,x0
  419.     move            x:y1_accumulator,a
  420.     cmp        x0,a    y:(r4+n4),x1
  421.     tmi        x0,a
  422.     cmp        x1,a    x:(r0+n0),x0
  423.     tpl        x1,a
  424.     move        a,y0
  425.     move            x:y2_accumulator,a
  426.     cmp        x0,a    y:height,x1
  427.     tmi        x0,a
  428.     cmp        x1,a    (r7)+
  429.     tpl        x1,a
  430.     move        a,y1
  431.     endm
  432.  
  433. ;-------------------------------------------------------*
  434.     org        p:0
  435. ;-------------------------------------------------------*
  436.     jmp        start
  437. ;-------------------------------------------------------*
  438.     org        p:64
  439. ;-------------------------------------------------------*
  440.  
  441. ;-------------------------------------------------------*
  442. ;    Complete section of floor or ceiling        *
  443. ;-------------------------------------------------------*
  444. end_flat:
  445. ;-------------------------------------------------------*
  446.     move        a,n6
  447.     move        b,n2    
  448.     add        a,b    y:(r1)+,x0
  449.     cmp        x0,a    y:(r1)-,x1
  450.     tpl        x0,a
  451.     cmp        x1,b    a,y:(r1)+
  452.     tmi        x1,b
  453.     move            b,y:(r1)
  454. ;-------------------------------------------------------*
  455.     move        y:flatstack_ptr,r6
  456.     move        y0,x:temp1        ; store old ytop
  457.     lua        (r6)+n6,r1
  458.     move        y:lastslots_ptr,r6
  459.     move        n0,x0
  460.     move        y:lshft12,x1
  461.     mpy        x0,x1,b        (r6)+n6
  462.     move        b0,b            ; b = (x2<<12)
  463.     move        y:x2_msk,y0        ; x0 = x2:x1 mergemask 
  464.     move        y:two,x0
  465.     move        r7,n6            ; store old occlusion-list
  466.     move        y:rundata_ptr,r7    
  467.     move        y:index,a
  468.     do        n2,_vert
  469.     move                    y:(r6),n2    ; lastindex = lastslot(y)
  470.     and        y0,b    x:(r1)+,x1    a,y:(r6)+    ; fetch x1 : lastslot(y) = index
  471.     or        x1,b    a,n7                ; rundata = ((x2<<12)|x1)
  472.     move            a1,x:(r2+n2)            ; runs(lastslot(y)(next)) = index
  473.     add        x0,a    b1,x:(r7+n7)            ; runs(index(data)) = rundata : index+runsize
  474. _vert:    move        a,y:index
  475.     move        n6,r7            ; store old occlusion-list
  476.     move        x:temp1,y0        ; store old ytop
  477.     rts
  478.  
  479. ;-------------------------------------------------------*
  480. ;    Generate & defragment a section of floor    *
  481. ;-------------------------------------------------------*
  482. getflatarea:
  483. ;-------------------------------------------------------*
  484.     jsr        new_luminance
  485.     move        y:runlink_ptr,r2        ; slotlink
  486.     move        y:rundata_ptr,r3        ; slotdata
  487.     move        y:two,n1
  488.     move        y:lastslots_ptr,r5    
  489.     move        y:HTX_ptr,r7
  490. ;-------------------------------------------------------*
  491.     move                y:(r0),b
  492.     dspwaitwrite
  493.     tfr        b,a        b,x:(r7)    ; [zone_start]
  494.     asl        b        a,n5
  495.     move                b,n2
  496.     move                y:(r4),b
  497.     sub        a,b        #0,x0
  498.     tmi        x0,b
  499.     dspwaitwrite
  500.     tst        b        b,x:(r7)    ; [zone_lines]
  501.     jeq        _vert
  502. ;-------------------------------------------------------*
  503.     lua        (r2)+n2,r1            ; firstlink
  504.     lua        (r5)+n5,r5            ; lastslot
  505.     move        y:x1list_ptr,r0
  506.     move        y:x2list_ptr,r4
  507. ;-------------------------------------------------------*
  508.     do        b,_vert
  509. ;-------------------------------------------------------*
  510. _runs:    move                y:(r5)+,n2    ; ls=lastslot(y)
  511.     clr        a        x:(r1)+n1,y1    ; fs=firstslot(y) : next firstslot-y
  512.     clr        b        a0,x:(r2+n2)    ; runs(ls(link)) = 0
  513.     cmp        y1,a        y1,n2        ; check for slot terminator
  514.     jeq        _get_runs            ; repeat until slots exhausted for this (y)
  515. ;-------------------------------------------------------*
  516.     move                x:t,x0
  517.     move                y:two,a
  518.     and        #$FE,ccr
  519.     rep        #24
  520.     div        x0,a
  521.     move        a0,x1
  522.     move                y:pnorm,x0
  523.     mpy        x0,x1,a        y:tlist_ptr,r6
  524.     move                a0,y:z
  525. ;-------------------------------------------------------*
  526. _slots:
  527. ;-------------------------------------------------------*
  528.     move        n2,n3
  529.     move                y:x1_msk,a    ; x-mask
  530.     move                x:(r3+n3),x0    ; x2x1 rundata
  531.     and        x0,a        y:lshft12,x1
  532.     mpy        x0,x1,a        a1,x0
  533.     cmp        x0,a        a1,x1
  534.     jle        _end_add
  535. ;-------------------------------------------------------*
  536. ;    Floor defragmentation algorithm            *
  537. ;-------------------------------------------------------*
  538. _add_run:
  539. ;-------------------------------------------------------*
  540.     move                y:oslist_ptr,r7
  541.     move                x1,n0
  542.     move                x0,n4
  543.     move                x0,x:(r0+n0)    ; x1list(x2c) = x1c
  544.     move                x1,x:(r4+n4)    ; x2list(x1c) = x2c
  545.     move                y:one,a
  546.     move                x0,n0
  547.     move                x1,n4
  548.     move                x:(r0+n0),y0    ; x1l = x1list(x1c)
  549.     cmp        y0,a        x:(r4+n4),y1    ; x2r = x2list(x2c)
  550.     jle        _left
  551.     cmp        y1,a
  552.     jle        _rightonly
  553. ;-------------------------------------------------------*
  554. _addnormal:
  555. ;-------------------------------------------------------*
  556.     move                x0,n7
  557.     move                b1,n6
  558.     add        a,b        b1,x:(r7+n7)    ; os(x1) = index
  559.     move                x0,y:(r6+n6)    ; track(index) = x1
  560.     jmp        _end_add
  561. ;-------------------------------------------------------*
  562. _left:    move                x1,n0
  563.     move                y0,n4
  564.     move                y0,x:(r0+n0)    ; x1list(x2c) = x1l
  565.     move                x1,x:(r4+n4)    ; x2list(x1l) = x2c
  566.     move                x0,n4
  567.     move                x0,n0
  568.     move                a0,x:(r4+n4)    ; x2list(x1c) = 0        
  569.     move                x1,n4
  570.     move                a0,x:(r0+n0)    ; x1list(x1c) = 0                
  571.     move                x:(r4+n4),y1    ; x2r = x2list(x2c)
  572.     cmp        y1,a        y0,x0        ; x1c = x1l
  573.     jgt        _leftonly
  574. ;-------------------------------------------------------*
  575. _both:
  576. ;-------------------------------------------------------*
  577.     move                x0,n4
  578.     move                y1,n0
  579.     move                y1,x:(r4+n4)    ; x2list(x1c) = x2r
  580.     move                x0,x:(r0+n0)    ; x1list(x2r) = x1c
  581.     move                x1,n4
  582.     move                x1,n0
  583.     sub        a,b        a0,x:(r4+n4)    ; x2list(x2c) = 0
  584.     move                x1,n7
  585.     move                b1,n6
  586.     move                x:(r7+n7),x0    ; index of right neigbour
  587.     move                y:(r6+n6),n7    ; index of last run
  588.     move                x0,n6
  589.     move                a0,x:(r0+n0)    ; x1list(x2c) = 0
  590.     move                n7,y:(r6+n6)    ; copy last index over right neighbour
  591.     move                n6,x:(r7+n7)    ; update run to point to new index
  592. ;-------------------------------------------------------*
  593. _leftonly:
  594. ;-------------------------------------------------------*
  595.     jmp        _end_add
  596. ;-------------------------------------------------------*
  597. _rightonly:
  598. ;-------------------------------------------------------*
  599.     move                x0,n4
  600.     move                y1,n0
  601.     move                y1,x:(r4+n4)    ; x2list(x1c) = x2r
  602.     move                x0,x:(r0+n0)    ; x1list(x2r) = x1c
  603.     move                x1,n7
  604.     move                x1,n0
  605.     move                x1,n4
  606.     move                a0,x:(r0+n0)    ; x1list(x2c) = 0
  607.     move                a0,x:(r4+n4)    ; x2list(x2c) = 0
  608.     move                x:(r7+n7),n6    ; index of right neighbour
  609.     move                x0,n7
  610.     move                x0,y:(r6+n6)    ; point index to new run
  611.     move                n6,x:(r7+n7)
  612. ;-------------------------------------------------------*
  613. _end_add:
  614. ;-------------------------------------------------------*
  615.     clr        a        x:(r2+n2),y1    ; locate next slot for this (y)
  616.     cmp        y1,a        y1,n2
  617.     jne        _slots
  618.     move                y:HTX_ptr,r7
  619. ;-------------------------------------------------------*
  620. _get_runs:
  621. ;-------------------------------------------------------*
  622.     dspwaitwrite
  623.     tst        b        b,x:(r7)
  624.     jeq        _line
  625. ;-------------------------------------------------------*
  626. ;    Send luminance back to host            *
  627. ;-------------------------------------------------------*
  628.     move                y:z,y0
  629.     tfr        y0,a        #0,x0
  630.     neg        a        y:lshft19,x1
  631.     add        x1,a        y:lshft5,x1
  632.     tmi        x0,a
  633.     move        a,x0
  634.     mpy        x0,x1,a        r1,n5
  635.     move        a0,x0
  636.     mpy        x0,x0,a        y:run_ptr,r1
  637.     move        a,x0
  638.     mpy        x0,x0,a        y:rshft5,x1
  639.     move        a,x0
  640.     mpy        x0,x1,a        y:mlum2,x1
  641.     move        a,x0
  642.     mpy        x0,x1,a        y:mlum3,x0
  643.     add        x0,a        #0,x1
  644.     tmi        x1,a
  645.     move                y:mlum_limit,x1
  646.     cmp        x1,a        y:one,x0
  647.     tpl        x1,a
  648. ;-------------------------------------------------------*
  649.     dspwaitwrite
  650.     clr        a        a,x:(r7)    
  651. ;-------------------------------------------------------*
  652. ;    Send joined runs back to host            *
  653. ;-------------------------------------------------------*
  654.     do        b1,_fetch
  655.     move                y:(r6)+,n4    ; n4 = x1
  656.     move        n4,a
  657.     move                x:(r4+n4),n0    ; n0 = x2 
  658.     move                a0,x:(r4+n4)
  659.     move                a0,x:(r0+n0)
  660.     sub        x0,a        n0,b
  661.     sub        x0,b        y0,y:(r1)+
  662.     dspwaitwrite
  663.     move        b,x:(r7)    a,y:(r1)+
  664. ;-------------------------------------------------------*
  665. _fetch:    move                r1,y:run_ptr
  666.     move                n5,r1
  667. ;-------------------------------------------------------*
  668. _line:    move                l:ti,a
  669.     move                l:t,b
  670.     add        a,b
  671.     clr        a        b,l:t
  672. ;-------------------------------------------------------*
  673. _vert:    rts
  674.  
  675. ;-------------------------------------------------------*
  676. ;    Calculate texture index for perspected wall    *
  677. ;-------------------------------------------------------*
  678. perspected_column:
  679. ;-------------------------------------------------------*
  680.     move                y:HTX_ptr,r1
  681. ;-------------------------------------------------------*
  682. ;    Write [i]                     *
  683. ;-------------------------------------------------------*
  684.     move                y:one,x0
  685.     move        n0,a
  686.     sub        x0,a        y:lshft8,x0
  687.     dspwaitwrite
  688.     mpy        x0,y0,a        a,x:(r1)
  689. ;-------------------------------------------------------*
  690. ;    Write [y1:y2] / calculate [u] factor        *
  691. ;-------------------------------------------------------*
  692.     move        a0,b
  693.     or        y1,b        x:t_accumulator,x0
  694.     move                y:lshft10,a
  695.     and        #$FE,ccr
  696.     rep        #24
  697.     div        x0,a
  698.     move        a0,a
  699.     move                y:wall_fsz,x1
  700.     sub        x1,a        y:wall_fdz,x1
  701.     abs        a        y:wall_dz,x0
  702.     dspwaitwrite
  703.     move                b,x:(r1)
  704. ;-------------------------------------------------------*
  705. ;    Calculate [u] index                *
  706. ;-------------------------------------------------------*
  707.     and        #$FE,ccr
  708.     rep        #24
  709.     div        x1,a
  710.     move        a0,x1
  711. ;-------------------------------------------------------*
  712. ;    Calculate [lum]                    *
  713. ;-------------------------------------------------------*
  714.     mpy        x0,x1,b        y:wall_z2,x0
  715.     sub        x0,b        y:lshft19,x1
  716.     move        #0,x0
  717.     add        x1,b        y:lshft5,x1
  718.     tmi        x0,b
  719.     move        b,x0
  720.     mpy        x0,x1,b
  721.     move        b0,x0
  722.     mpy        x0,x0,b
  723.     move        b,x0
  724.     mpy        x0,x0,b        y:rshft5,x1
  725.     move        b,x0
  726.     mpy        x0,x1,b        y:mlum2,x1
  727.     move        b,x0
  728.     mpy        x0,x1,b        y:mlum3,x0
  729.     add        x0,b        #0,x1
  730.     tmi        x1,b
  731.     move                y:mlum_limit,x1
  732.     cmp        x1,b        y:lshft6,x0        
  733.     tpl        x1,b
  734.     move        b,x1
  735.     mpy        x0,x1,b        a0,x1
  736. ;-------------------------------------------------------*
  737.     dspwaitwrite
  738.     move                b0,x:(r1)    
  739. ;-------------------------------------------------------*
  740.     move                y:wall_umag,x0
  741.     mpy        -x0,x1,a    y:wall_u,x0
  742.     sub        x0,a        y:rshft8,x0
  743.     move        a,x1
  744.     mpy        x0,x1,a        y:wall_usize,x0
  745.     and        x0,a        y:wall_vsize,x1
  746.     move        a1,x0
  747.     mpy        x0,x1,a
  748.     asr        a        l:y2_accumulator,b
  749. ;-------------------------------------------------------*
  750. ;    Write [u]                    *
  751. ;-------------------------------------------------------*
  752.     dspwaitwrite
  753.     move                a0,x:(r1)
  754. ;-------------------------------------------------------*
  755. ;    Calculate [dv]                    *
  756. ;-------------------------------------------------------*
  757.     move                l:y1_accumulator,a
  758.     sub        a,b        y:wall_vmag,a
  759.     rep        #8
  760.     asl        b
  761.     asl        a        b,x1
  762.     and        #$FE,ccr
  763.     rep        #24
  764.     div        x1,a
  765.     dspwaitwrite
  766.     move                a0,x:(r1)
  767. ;-------------------------------------------------------*
  768. ;    Calculate [v] index                *
  769. ;-------------------------------------------------------*
  770.     move                l:y1_accumulator,b
  771.     sub        y0,b        a0,x1
  772.     neg        b        y:one,x0
  773.     add        x0,b
  774.     rep        #8
  775.     asl        b
  776.     clr        b        b,x0
  777.     mpy        x0,x1,a        y:wall_voff,x0
  778.     add        x0,a
  779.     rep        #8-1
  780.     asl        a
  781.     dspwaitwrite
  782.     clr        a        a1,x:(r1)
  783.     rts
  784.  
  785. ;-------------------------------------------------------*
  786. ;    Calculate texture index for linear wall        *
  787. ;-------------------------------------------------------*
  788. linear_column:
  789. ;-------------------------------------------------------*
  790.     move                y:HTX_ptr,r1
  791. ;-------------------------------------------------------*
  792. ;    Write [i]                     *
  793. ;-------------------------------------------------------*
  794.     move                y:one,x0
  795.     move        n0,a
  796.     sub        x0,a        y:lshft8,x0
  797.     dspwaitwrite
  798.     mpy        x0,y0,a        a,x:(r1)
  799. ;-------------------------------------------------------*
  800. ;    Write [y1:y2] / calculate [u] factor        *
  801. ;-------------------------------------------------------*
  802.     move        a0,b
  803.     or        y1,b        x:t_accumulator,a
  804.     dspwaitwrite
  805.     move                b,x:(r1)
  806.     move                y:wall_sz,x1
  807.     sub        x1,a        y:wall_dz,x1    ; ez-sz
  808.     abs        a        y:wall_z1,b
  809. ;-------------------------------------------------------*
  810. ;    Calculate [u] index                *
  811. ;-------------------------------------------------------*
  812.     and        #$FE,ccr
  813.     rep        #24
  814.     div        x1,a
  815.     neg        b        y:lshft19,x1
  816.     move        #0,x0
  817.     add        x1,b        y:lshft5,x1
  818.     tmi        x0,b
  819.     move        b,x0
  820.     mpy        x0,x1,b
  821.     move        b0,x0
  822.     mpy        x0,x0,b
  823.     move        b,x0
  824.     mpy        x0,x0,b        y:rshft5,x1
  825.     move        b,x0
  826.     mpy        x0,x1,b        y:mlum2,x1
  827.     move        b,x0
  828.     mpy        x0,x1,b        y:mlum3,x0
  829.     add        x0,b        #0,x1
  830.     tmi        x1,b
  831.     move                y:mlum_limit,x1
  832.     cmp        x1,b        y:lshft6,x0        
  833.     tpl        x1,b
  834.     move        b,x1
  835.     mpy        x0,x1,b        a0,x1
  836. ;-------------------------------------------------------*
  837.     dspwaitwrite
  838.     move                b0,x:(r1)    
  839. ;-------------------------------------------------------*
  840.     move                y:wall_umag,x0
  841.     mpy        -x0,x1,a    y:wall_u,x0
  842.     sub        x0,a        y:rshft8,x0
  843.     move        a,x1
  844.     mpy        x0,x1,a        y:wall_usize,x0
  845.     and        x0,a        y:wall_vsize,x1
  846.     move        a1,x0
  847.     mpy        x0,x1,a
  848.     asr        a        l:y2_accumulator,b
  849. ;-------------------------------------------------------*
  850. ;    Write [u]                    *
  851. ;-------------------------------------------------------*
  852.     dspwaitwrite
  853.     move                a0,x:(r1)
  854. ;-------------------------------------------------------*
  855. ;    Calculate [dv]                    *
  856. ;-------------------------------------------------------*
  857.     move                l:y1_accumulator,a
  858.     sub        a,b        y:wall_vmag,a
  859.     rep        #8
  860.     asl        b
  861.     asl        a        b,x1
  862.     and        #$FE,ccr
  863.     rep        #24
  864.     div        x1,a
  865.     dspwaitwrite
  866.     move                a0,x:(r1)
  867. ;-------------------------------------------------------*
  868. ;    Calculate [v] index                *
  869. ;-------------------------------------------------------*
  870.     move                l:y1_accumulator,b
  871.     sub        y0,b        a0,x1
  872.     neg        b        y:one,x0
  873.     add        x0,b
  874.     rep        #8
  875.     asl        b
  876.     clr        b        b,x0
  877.     mpy        x0,x1,a        y:wall_voff,x0
  878.     add        x0,a
  879.     rep        #8-1
  880.     asl        a
  881.     dspwaitwrite
  882.     clr        a        a1,x:(r1)
  883.     rts
  884.  
  885. ;-------------------------------------------------------*
  886. AddMidWall:
  887. ;-------------------------------------------------------*
  888.     jsr        init_addwall
  889. ;-------------------------------------------------------*
  890.     do        b,midwall_loop
  891. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  892. ;    Advance edge accumulators            *
  893. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  894.     load_column
  895. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  896. ;    Generate floor buckets                *
  897. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  898. ;    clip if [thisbot]=>[thismaxy]            *
  899. ;-------------------------------------------------------*
  900.     tfr        y1,b    y:(r4+n4),a        ; [thisbot] : [thismaxy]
  901.     cmp        a,b
  902.     jpl        _clip_lower
  903. ;-------------------------------------------------------*
  904. ;    draw if [lastbot]>[thisbot]            *
  905. ;-------------------------------------------------------*
  906.     tfr        y1,a    y:(r5+n5),b        ; [thisbot] : [lastbot]
  907.     sub        a,b
  908.     jsgt        start_floor            ; y1=[thisbot] : y2=[lastbot]
  909. ;-------------------------------------------------------*
  910. ;    draw if [thismaxy]>[lastmaxy]            *
  911. ;-------------------------------------------------------*
  912.     move        y:(r4),a            ; [lastmaxy]
  913.     move        y:(r4+n4),b            ; [thismaxy]
  914.     sub        a,b
  915.     jsgt        start_floor            ; y1=[lastmaxy] : y2=[thismaxy]
  916. ;-------------------------------------------------------*
  917. ;    draw if [thisbot]>[lastbot]            *
  918. ;-------------------------------------------------------*
  919.     tfr        y1,b    y:(r5+n5),a        ; [thisbot] : [lastbot]
  920.     sub        a,b    #floor_bounds,r1
  921.     jsgt        end_flat            ; y1=[thisbot] : y2=[lastbot]
  922. ;-------------------------------------------------------*
  923. ;    draw if [lastmaxy]>[thismaxy]            *
  924. ;-------------------------------------------------------*
  925.     move        y:(r4+n4),a            ; [thismaxy]
  926.     move        y:(r4),b            ; [lastmaxy]
  927.     sub        a,b    #floor_bounds,r1
  928.     jsgt        end_flat            ; y1=[thismaxy] : y2=[lastmaxy]
  929. ;-------------------------------------------------------*
  930.     jmp        _floor_done
  931. ;-------------------------------------------------------*
  932. _clip_lower:
  933. ;-------------------------------------------------------*
  934. ;    draw if [lastmaxy]>[lastbot]            *
  935. ;-------------------------------------------------------*
  936.     move        y:(r5+n5),a            ; [lastbot]
  937.     move        y:(r4),b            ; [lastmaxy]
  938.     sub        a,b    #floor_bounds,r1
  939.     jsgt        end_flat            ; y1=[lastbot] : y2=[lastmaxy]
  940. ;-------------------------------------------------------*
  941. _floor_done:    
  942. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  943. ;    Generate ceiling buckets            *
  944. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  945. ;    clip if [thistop]<=[thisminy]            *
  946. ;-------------------------------------------------------*
  947.     tfr        y0,b    x:(r0+n0),a        ; [thistop] : [thisminy]
  948.     cmp        a,b
  949.     jle        _clip_upper    
  950. ;-------------------------------------------------------*
  951. ;    draw if [thistop]>[lasttop]            *
  952. ;-------------------------------------------------------*
  953.     tfr        y0,b    x:(r3+n3),a        ; [thistop] : [lasttop]
  954.     sub        a,b
  955.     jsgt        start_ceiling            ; y1=[lasttop] : y2=[thistop]
  956. ;-------------------------------------------------------*
  957. ;    draw if [lastminy]>[thisminy]            *
  958. ;-------------------------------------------------------*
  959.     move        x:(r0+n0),a            ; [thisminy]
  960.     move        x:(r0),b            ; [lastminy]
  961.     sub        a,b
  962.     jsgt        start_ceiling            ; y1=[thisminy] : y2=[lastminy]
  963. ;-------------------------------------------------------*
  964. ;    draw if [lasttop]>[thistop]            *
  965. ;-------------------------------------------------------*
  966.     tfr        y0,a    x:(r3+n3),b        ; [thistop] : [lasttop]
  967.     sub        a,b    #ceiling_bounds,r1
  968.     jsgt        end_flat            ; y1=[thistop] : y2=[lasttop]
  969. ;-------------------------------------------------------*
  970. ;    draw if [thisminy]>[lastminy]            *
  971. ;-------------------------------------------------------*
  972.     move        x:(r0),a            ; [lastminy]
  973.     move        x:(r0+n0),b            ; [thisminy]
  974.     sub        a,b    #ceiling_bounds,r1
  975.     jsgt        end_flat            ; y1=[lastminy] : y2=[thisminy]
  976. ;-------------------------------------------------------*
  977.     jmp        _ceiling_done
  978. ;-------------------------------------------------------*
  979. _clip_upper:
  980. ;-------------------------------------------------------*
  981.     move        x:(r0),a            ; [lastminy]
  982.     move        x:(r3+n3),b            ; [lasttop]
  983.     sub        a,b    #ceiling_bounds,r1
  984.     jsgt        end_flat            ; y1=[lastminy] : y2=[lasttop]
  985. ;-------------------------------------------------------*
  986. _ceiling_done:    
  987. ;-------------------------------------------------------*
  988. ;    Rotate column history registers            *
  989. ;-------------------------------------------------------*
  990. ;    lasttop = thistop / lastbot = thisbot        *
  991. ;-------------------------------------------------------*
  992.     move        y0,x:(r3+n3)
  993.     move        y1,y:(r5+n5)
  994. ;-------------------------------------------------------*
  995. ;    lastmin = thismin / lastmax = thismax        *
  996. ;-------------------------------------------------------*
  997.     move        x:(r0+n0),a
  998.     move        y:(r4+n4),b
  999. ;-------------------------------------------------------*
  1000. ;    Check - column is already sealed off        *
  1001. ;-------------------------------------------------------*
  1002.     cmp        a,b    a,x:(r0)    b,y:(r4)
  1003.     jle        _skip
  1004. ;-------------------------------------------------------*
  1005. ;    Check - column below [maxy] - blocked by roof    *
  1006. ;-------------------------------------------------------*
  1007.     cmp        y0,b
  1008.     jle        _update_window
  1009. ;-------------------------------------------------------*
  1010. ;    Check - column above [miny] - blocked by floor    *
  1011. ;-------------------------------------------------------*
  1012.     cmp        y1,a
  1013.     jpl        _update_window
  1014. ;-------------------------------------------------------*
  1015. ;    Clip - top of column cropped by [miny]        *
  1016. ;-------------------------------------------------------*
  1017.     cmp        y0,a
  1018.     jle        _ncl1
  1019.     move        a,y0
  1020. ;-------------------------------------------------------*
  1021. ;    Clip - bottom of column cropped by [maxy]    *
  1022. ;-------------------------------------------------------*
  1023. _ncl1:    cmp        y1,b 
  1024.     jpl        _ncl2
  1025.     move        b,y1
  1026. ;-------------------------------------------------------*
  1027. ;    Check - column cropped to zero length        *
  1028. ;-------------------------------------------------------*
  1029. _ncl2:    tfr        y1,b    y:column_rout,r1
  1030.     cmp        y0,b    n0,x0            ; [thistop]<>[thisbot]
  1031.     jsgt        (r1)
  1032. ;-------------------------------------------------------*
  1033. ;    Update vertical clipping bar            *
  1034. ;-------------------------------------------------------*
  1035. _update_window:
  1036. ;-------------------------------------------------------*
  1037.     tfr        y0,a    y1,b
  1038. ;-------------------------------------------------------*
  1039.     clr        b    y:height,a
  1040.     move            a,x:(r0+n0)
  1041.     move            b,y:(r4+n4)
  1042.     move        #1,a1
  1043.     move            y:columns,b
  1044.     sub        a,b    b0,x:(r7)
  1045.     move            b,y:columns        
  1046. ;-------------------------------------------------------*
  1047. ;    Advance to next column                *
  1048. ;-------------------------------------------------------*
  1049. _skip:    advance_column
  1050. ;-------------------------------------------------------*
  1051. midwall_loop:
  1052. ;-------------------------------------------------------*
  1053. ;    Fill gaps left in floor & ceiling by edges    *
  1054. ;-------------------------------------------------------*
  1055.     move        y:(r5+n5),a            ; [lastbot]
  1056.     move        y:(r4),b            ; [lastmaxy]
  1057.     sub        a,b    #floor_bounds,r1
  1058.     jsgt        end_flat
  1059.     move        x:(r0),a            ; [lastminy]
  1060.     move        x:(r3+n3),b            ; [lasttop]
  1061.     sub        a,b    #ceiling_bounds,r1
  1062.     jsgt        end_flat
  1063. ;-------------------------------------------------------*
  1064.     jmp        end_addwall
  1065.  
  1066. ;-------------------------------------------------------*
  1067. AddTransWall:
  1068. ;-------------------------------------------------------*
  1069.     jsr        init_addwall
  1070. ;-------------------------------------------------------*
  1071.     do        b,trans_loop
  1072. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1073. ;    Advance edge accumulators            *
  1074. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1075.     load_column
  1076. ;-------------------------------------------------------*
  1077. ;    lastmin = thismin / lastmax = thismax        *
  1078. ;-------------------------------------------------------*
  1079.     move        x:(r0+n0),a
  1080.     move        y:(r4+n4),b
  1081. ;-------------------------------------------------------*
  1082. ;    Check - column is already sealed off        *
  1083. ;-------------------------------------------------------*
  1084.     cmp        a,b
  1085.     jle        _skip
  1086. ;-------------------------------------------------------*
  1087. ;    Check - column below [maxy] - blocked by roof    *
  1088. ;-------------------------------------------------------*
  1089.     cmp        y0,b
  1090.     jle        _advance
  1091. ;-------------------------------------------------------*
  1092. ;    Check - column above [miny] - blocked by floor    *
  1093. ;-------------------------------------------------------*
  1094.     cmp        y1,a
  1095.     jpl        _advance
  1096. ;-------------------------------------------------------*
  1097. ;    Clip - top of column cropped by [miny]        *
  1098. ;-------------------------------------------------------*
  1099.     cmp        y0,a
  1100.     jle        _ncl1
  1101.     move        a,y0
  1102. ;-------------------------------------------------------*
  1103. ;    Clip - bottom of column cropped by [maxy]    *
  1104. ;-------------------------------------------------------*
  1105. _ncl1:    cmp        y1,b 
  1106.     jpl        _ncl2
  1107.     move        b,y1
  1108. ;-------------------------------------------------------*
  1109. ;    Check - column cropped to zero length        *
  1110. ;-------------------------------------------------------*
  1111. _ncl2:    tfr        y1,b    y:column_rout,r1
  1112.     cmp        y0,b    n0,x0            ; [thistop]<>[thisbot]
  1113.     jsgt        (r1)
  1114. ;-------------------------------------------------------*
  1115. ;    Update vertical clipping bar            *
  1116. ;-------------------------------------------------------*
  1117. _advance:
  1118. ;-------------------------------------------------------*
  1119. ;    Advance to next column                *
  1120. ;-------------------------------------------------------*
  1121. _skip:    advance_column
  1122. ;-------------------------------------------------------*
  1123. trans_loop:
  1124. ;-------------------------------------------------------*
  1125.     jmp        end_addwall
  1126.  
  1127. ;-------------------------------------------------------*
  1128. AddUpperWall:
  1129. ;-------------------------------------------------------*
  1130.     jsr        init_addwall
  1131. ;-------------------------------------------------------*
  1132.     do        b,upperwall_loop
  1133. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1134. ;    Advance edge accumulators            *
  1135. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1136.     load_column
  1137. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1138. ;    Generate ceiling buckets            *
  1139. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1140. ;    clip if [thistop]<=[thisminy]            *
  1141. ;-------------------------------------------------------*
  1142.     tfr        y0,b    x:(r0+n0),a        ; [thistop] : [thisminy]
  1143.     cmp        a,b
  1144.     jle        _clip_upper    
  1145. ;-------------------------------------------------------*
  1146. ;    draw if [thistop]>[lasttop]            *
  1147. ;-------------------------------------------------------*
  1148.     tfr        y0,b    x:(r3+n3),a        ; [thistop] : [lasttop]
  1149.     sub        a,b
  1150.     jsgt        start_ceiling            ; y1=[lasttop] : y2=[thistop]
  1151. ;-------------------------------------------------------*
  1152. ;    draw if [lastminy]>[thisminy]            *
  1153. ;-------------------------------------------------------*
  1154.     move        x:(r0+n0),a            ; [thisminy]
  1155.     move        x:(r0),b            ; [lastminy]
  1156.     sub        a,b
  1157.     jsgt        start_ceiling            ; y1=[thisminy] : y2=[lastminy]
  1158. ;-------------------------------------------------------*
  1159. ;    draw if [lasttop]>[thistop]            *
  1160. ;-------------------------------------------------------*
  1161.     tfr        y0,a    x:(r3+n3),b        ; [thistop] : [lasttop]
  1162.     sub        a,b    #ceiling_bounds,r1
  1163.     jsgt        end_flat            ; y1=[thistop] : y2=[lasttop]
  1164. ;-------------------------------------------------------*
  1165. ;    draw if [thisminy]>[lastminy]            *
  1166. ;-------------------------------------------------------*
  1167.     move        x:(r0),a            ; [lastminy]
  1168.     move        x:(r0+n0),b            ; [thisminy]
  1169.     sub        a,b    #ceiling_bounds,r1
  1170.     jsgt        end_flat            ; y1=[lastminy] : y2=[thisminy]
  1171. ;-------------------------------------------------------*
  1172.     jmp        _ceiling_done
  1173. ;-------------------------------------------------------*
  1174. _clip_upper:
  1175. ;-------------------------------------------------------*
  1176.     move        x:(r0),a            ; [lastminy]
  1177.     move        x:(r3+n3),b            ; [lasttop]
  1178.     sub        a,b    #ceiling_bounds,r1
  1179.     jsgt        end_flat            ; y1=[lastminy] : y2=[lasttop]
  1180. ;-------------------------------------------------------*
  1181. _ceiling_done:    
  1182. ;-------------------------------------------------------*
  1183. ;    Rotate column history registers            *
  1184. ;-------------------------------------------------------*
  1185. ;    lasttop = thistop / lastbot = thisbot        *
  1186. ;-------------------------------------------------------*
  1187.     move        y0,x:(r3+n3)
  1188.     move        y1,y:(r5+n5)
  1189. ;-------------------------------------------------------*
  1190. ;    lastmin = thismin / lastmax = thismax        *
  1191. ;-------------------------------------------------------*
  1192.     move        x:(r0+n0),a
  1193.     move        y:(r4+n4),b
  1194. ;-------------------------------------------------------*
  1195. ;    Check - column is already sealed off        *
  1196. ;-------------------------------------------------------*
  1197.     cmp        a,b    a,x:(r0)    b,y:(r4)
  1198.     jle        _skip
  1199. ;-------------------------------------------------------*
  1200. ;    Check - column below [maxy] - blocked by roof    *
  1201. ;-------------------------------------------------------*
  1202.     cmp        y1,a
  1203.     jpl        _skip
  1204. ;-------------------------------------------------------*
  1205. ;    Check - column above [miny] - blocked by floor    *
  1206. ;-------------------------------------------------------*
  1207.     cmp        y0,b
  1208.     jle        _update_window
  1209. ;-------------------------------------------------------*
  1210. ;    Clip - top of column cropped by [miny]        *
  1211. ;-------------------------------------------------------*
  1212.     cmp        y0,a
  1213.     jle        _ncl1
  1214.     move        a,y0
  1215. ;-------------------------------------------------------*
  1216. ;    Clip - bottom of column cropped by [maxy]    *
  1217. ;-------------------------------------------------------*
  1218. _ncl1:    cmp        y1,b 
  1219.     jpl        _ncl2
  1220.     move        b,y1
  1221. ;-------------------------------------------------------*
  1222. ;    Check - column cropped to zero length        *
  1223. ;-------------------------------------------------------*
  1224. _ncl2:    tfr        y1,b    y:column_rout,r1
  1225.     cmp        y0,b    n0,x0            ; [thistop]<>[thisbot]
  1226.     jsgt        (r1)
  1227. ;-------------------------------------------------------*
  1228. ;    Update vertical clipping bar            *
  1229. ;-------------------------------------------------------*
  1230. _update_window:
  1231. ;-------------------------------------------------------*
  1232.     move            y:(r4+n4),b
  1233.     cmp        y1,b    #1,a1
  1234.     jgt        _upd
  1235.     move            y:columns,b
  1236.     sub        a,b    b0,x:(r7)
  1237.     move            b,y:columns
  1238. _cont:    move            b0,y:(r4+n4)
  1239.     move            y:height,y1
  1240. _upd:    move            y1,x:(r0+n0)
  1241. ;-------------------------------------------------------*
  1242. ;    Advance to next column                *
  1243. ;-------------------------------------------------------*
  1244. _skip:    advance_column
  1245. ;-------------------------------------------------------*
  1246. upperwall_loop:
  1247. ;-------------------------------------------------------*
  1248. ;    Fill gaps left in floor & ceiling by edges    *
  1249. ;-------------------------------------------------------*
  1250.     move        x:(r0),a
  1251.     move        x:(r3+n3),b
  1252.     sub        a,b    #ceiling_bounds,r1
  1253.     jsgt        end_flat
  1254. ;-------------------------------------------------------*
  1255.     jmp        end_addwall
  1256.  
  1257. ;-------------------------------------------------------*
  1258. AddLowerWall:
  1259. ;-------------------------------------------------------*
  1260.     jsr        init_addwall
  1261. ;-------------------------------------------------------*
  1262.     do        b,lowerwall_loop
  1263. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1264. ;    Advance edge accumulators            *
  1265. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1266.     load_column
  1267. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1268. ;    Generate floor buckets                *
  1269. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1270. ;    clip if [thisbot]=>[thismaxy]            *
  1271. ;-------------------------------------------------------*
  1272.     tfr        y1,b    y:(r4+n4),a        ; [thisbot] : [thismaxy]
  1273.     cmp        a,b
  1274.     jpl        _clip_lower
  1275. ;-------------------------------------------------------*
  1276. ;    draw if [lastbot]>[thisbot]            *
  1277. ;-------------------------------------------------------*
  1278.     tfr        y1,a    y:(r5+n5),b        ; [thisbot] : [lastbot]
  1279.     sub        a,b
  1280.     jsgt        start_floor            ; y1=[thisbot] : y2=[lastbot]
  1281. ;-------------------------------------------------------*
  1282. ;    draw if [thismaxy]>[lastmaxy]            *
  1283. ;-------------------------------------------------------*
  1284.     move        y:(r4),a            ; [lastmaxy]
  1285.     move        y:(r4+n4),b            ; [thismaxy]
  1286.     sub        a,b
  1287.     jsgt        start_floor            ; y1=[lastmaxy] : y2=[thismaxy]
  1288. ;-------------------------------------------------------*
  1289. ;    draw if [thisbot]>[lastbot]            *
  1290. ;-------------------------------------------------------*
  1291.     tfr        y1,b    y:(r5+n5),a        ; [thisbot] : [lastbot]
  1292.     sub        a,b    #floor_bounds,r1
  1293.     jsgt        end_flat            ; y1=[thisbot] : y2=[lastbot]
  1294. ;-------------------------------------------------------*
  1295. ;    draw if [lastmaxy]>[thismaxy]            *
  1296. ;-------------------------------------------------------*
  1297.     move        y:(r4+n4),a            ; [thismaxy]
  1298.     move        y:(r4),b            ; [lastmaxy]
  1299.     sub        a,b    #floor_bounds,r1
  1300.     jsgt        end_flat            ; y1=[thismaxy] : y2=[lastmaxy]
  1301. ;-------------------------------------------------------*
  1302.     jmp        _floor_done
  1303. ;-------------------------------------------------------*
  1304. _clip_lower:
  1305. ;-------------------------------------------------------*
  1306. ;    draw if [lastmaxy]>[lastbot]            *
  1307. ;-------------------------------------------------------*
  1308.     move        y:(r5+n5),a            ; [lastbot]
  1309.     move        y:(r4),b            ; [lastmaxy]
  1310.     sub        a,b    #floor_bounds,r1
  1311.     jsgt        end_flat            ; y1=[lastbot] : y2=[lastmaxy]
  1312. ;-------------------------------------------------------*
  1313. _floor_done:    
  1314. ;-------------------------------------------------------*
  1315. ;    Rotate column history registers            *
  1316. ;-------------------------------------------------------*
  1317. ;    lasttop = thistop / lastbot = thisbot        *
  1318. ;-------------------------------------------------------*
  1319.     move        y0,x:(r3+n3)
  1320.     move        y1,y:(r5+n5)
  1321. ;-------------------------------------------------------*
  1322. ;    lastmin = thismin / lastmax = thismax        *
  1323. ;-------------------------------------------------------*
  1324.     move        x:(r0+n0),a
  1325.     move        y:(r4+n4),b
  1326. ;-------------------------------------------------------*
  1327. ;    Check - column is already sealed off        *
  1328. ;-------------------------------------------------------*
  1329.     cmp        a,b    a,x:(r0)    b,y:(r4)
  1330.     jle        _skip
  1331. ;-------------------------------------------------------*
  1332. ;    Check - column below [maxy] - blocked by roof    *
  1333. ;-------------------------------------------------------*
  1334.     cmp        y0,b
  1335.     jle        _skip
  1336. ;-------------------------------------------------------*
  1337. ;    Check - column above [miny] - blocked by floor    *
  1338. ;-------------------------------------------------------*
  1339.     cmp        y1,a
  1340.     jpl        _update_window
  1341. ;-------------------------------------------------------*
  1342. ;    Clip - top of column cropped by [miny]        *
  1343. ;-------------------------------------------------------*
  1344.     cmp        y0,a
  1345.     jle        _ncl1
  1346.     move        a,y0
  1347. ;-------------------------------------------------------*
  1348. ;    Clip - bottom of column cropped by [maxy]    *
  1349. ;-------------------------------------------------------*
  1350. _ncl1:    cmp        y1,b 
  1351.     jpl        _ncl2
  1352.     move        b,y1
  1353. ;-------------------------------------------------------*
  1354. ;    Check - column cropped to zero length        *
  1355. ;-------------------------------------------------------*
  1356. _ncl2:    tfr        y1,b    y:column_rout,r1
  1357.     cmp        y0,b    n0,x0            ; [thistop]<>[thisbot]
  1358.     jsgt        (r1)
  1359. ;-------------------------------------------------------*
  1360. ;    Update vertical clipping bar            *
  1361. ;-------------------------------------------------------*
  1362. _update_window:
  1363. ;-------------------------------------------------------*
  1364.     move            x:(r0+n0),b
  1365.     cmp        y0,b    #1,a1
  1366.     jlt        _upd
  1367.     move            y:columns,b
  1368.     sub        a,b    b0,x:(r7)
  1369.     move            b,y:columns
  1370. _cont:    move            y:height,y1
  1371.     move            y1,x:(r0+n0)
  1372.     move            b0,y0    
  1373. _upd:    move            y0,y:(r4+n4)
  1374. ;-------------------------------------------------------*
  1375. ;    Advance to next column                *
  1376. ;-------------------------------------------------------*
  1377. _skip:    advance_column
  1378. ;-------------------------------------------------------*
  1379. lowerwall_loop:
  1380. ;-------------------------------------------------------*
  1381. ;    Fill gaps left in floor & ceiling by edges    *
  1382. ;-------------------------------------------------------*
  1383.     move        y:(r5+n5),a
  1384.     move        y:(r4),b
  1385.     sub        a,b    #floor_bounds,r1
  1386.     jsgt        end_flat
  1387. ;-------------------------------------------------------*
  1388.     jmp        end_addwall
  1389.  
  1390. ;-------------------------------------------------------*
  1391. end_addwall:
  1392. ;-------------------------------------------------------*
  1393.     move        #-1,a1
  1394.     dspwrite    a1
  1395.     dspwrite    y:columns
  1396.     jmp        command_base
  1397.  
  1398. ;-------------------------------------------------------*
  1399. ;    Begin section of floor or ceiling        *
  1400. ;-------------------------------------------------------*
  1401. start_ceiling:
  1402. ;-------------------------------------------------------*
  1403. start_floor:
  1404. ;-------------------------------------------------------*
  1405.     move        y:flatstack_ptr,r1
  1406.     move        a,n1
  1407.     add        a,b    b1,n2
  1408.     lua        (r1)+n1,r1
  1409.     rep        n2
  1410.     move        n0,x:(r1)+    
  1411.     rts
  1412.  
  1413. ;-------------------------------------------------------*
  1414. ;    Get ready for wall column generator        *
  1415. ;-------------------------------------------------------*
  1416. init_addwall:
  1417. ;-------------------------------------------------------*
  1418. ;    Get wall details                *    
  1419. ;-------------------------------------------------------*
  1420.     move        y:HTX_ptr,r7
  1421.     move        #>1<<(8-1),x0
  1422.     move        #<$FF,b
  1423. ;-------------------------------------------------------*
  1424. ;    Read y-increments & accumulators        *
  1425. ;-------------------------------------------------------*
  1426.     move        #y1_accumulator,r6
  1427.     jsr        read_real
  1428.     move        #y1_increment,r6
  1429.     jsr        read_real
  1430.     move        #y2_accumulator,r6
  1431.     jsr        read_real
  1432.     move        #y2_increment,r6
  1433.     jsr        read_real
  1434. ;-------------------------------------------------------*
  1435.     move        #temp1,r6
  1436. ;-------------------------------------------------------*
  1437. ;    Read z-increments & accumulators        *
  1438. ;-------------------------------------------------------*
  1439.     jsr        read_real
  1440.     move        l:(r6),a
  1441.     rep        #8
  1442.     asl        a
  1443.     move        a,l:t_accumulator
  1444.     jsr        read_real
  1445.     move        l:(r6),a
  1446.     rep        #8
  1447.     asl        a
  1448.     move        a,l:t_increment
  1449.     asr        a
  1450.     move        l:t_accumulator,b
  1451.     add        a,b
  1452.     move        b,l:t_accumulator
  1453.     move        #<$FF,b
  1454. ;-------------------------------------------------------*
  1455. ;    Read z-bounds                    *
  1456. ;-------------------------------------------------------*
  1457.     jsr        read_real
  1458.     move        l:(r6),a
  1459.     rep        #8
  1460.     asl        a
  1461.     move        a,y:wall_z1
  1462.     jsr         read_real
  1463.     move        l:(r6),a
  1464.     rep        #8
  1465.     asl        a
  1466.     move        a,y:wall_z2
  1467. ;-------------------------------------------------------*
  1468. ;    Read texture buffer width            *
  1469. ;-------------------------------------------------------*
  1470.     dspread        a
  1471.     move        y:one,x0
  1472.     sub        x0,a
  1473.     move        a,y:wall_usize
  1474. ;-------------------------------------------------------*
  1475. ;    Read texture buffer height            *
  1476. ;-------------------------------------------------------*
  1477.     dspread        y:wall_vsize
  1478. ;-------------------------------------------------------*
  1479. ;    Read wall length                *
  1480. ;-------------------------------------------------------*
  1481.     dspread        y:wall_umag
  1482. ;-------------------------------------------------------*
  1483. ;    Read wall clipped portion            *
  1484. ;-------------------------------------------------------*
  1485.     dspread        y:wall_u
  1486. ;-------------------------------------------------------*
  1487. ;    Read wall height                *
  1488. ;-------------------------------------------------------*
  1489.     dspread        y:wall_vmag
  1490. ;-------------------------------------------------------*
  1491. ;    Read wall initial y-offset            *
  1492. ;-------------------------------------------------------*
  1493.     dspread        y:wall_voff
  1494. ;-------------------------------------------------------*
  1495.  
  1496. ;-------------------------------------------------------*
  1497.     dspread        y:ci1
  1498. ;-------------------------------------------------------*
  1499.     move        y:wall_z1,a
  1500.     move        a,y:wall_sz
  1501.     abs        a        a,y:0
  1502.     move        a,x0
  1503.     move        y:lshft10,a
  1504.     and        #$FE,ccr
  1505.     rep        #24
  1506.     div        x0,a
  1507.     move        a0,a
  1508.     jclr        #23,y:0,_p1
  1509.     neg        a
  1510. _p1:    move        a,y:wall_fsz
  1511. ;-------------------------------------------------------*
  1512.     dspread        y:ci2
  1513. ;-------------------------------------------------------*
  1514.     move        y:wall_z2,a
  1515.     move        y:wall_z1,b
  1516.     sub        b,a
  1517.     move        a,y:wall_dz
  1518.     move        y:wall_z1,a
  1519.     abs        a        a,y:0
  1520.     move        a,x0
  1521.     move        y:lshft10,a
  1522.     and        #$FE,ccr
  1523.     rep        #24
  1524.     div        x0,a
  1525.     move        a0,b
  1526.     jclr        #23,y:0,_p2
  1527.     neg        b
  1528. _p2:    move        y:wall_z2,a
  1529.     abs        a        a,y:0
  1530.     move        a,x0
  1531.     move        y:lshft10,a
  1532.     and        #$FE,ccr
  1533.     rep        #24
  1534.     div        x0,a
  1535.     move        a0,a
  1536.     jclr        #23,y:0,_p3
  1537.     neg        a
  1538. _p3:    sub        b,a
  1539.     move        a,y:wall_fdz
  1540. ;-------------------------------------------------------*
  1541. ;    Read x1 & x2                    *
  1542. ;-------------------------------------------------------*
  1543.     dspread        y:attributes
  1544.     dspread        y:columns
  1545. ;-------------------------------------------------------*
  1546.     move        #column_thistop,r3
  1547.     move        #column_thisbot,r5
  1548.     move        #1,n3
  1549.     move        #1,n5
  1550. ;-------------------------------------------------------*
  1551. ;    Init miny maxy & occlusion lists        *
  1552. ;-------------------------------------------------------*
  1553.     clr        b    #(occlusion_list-1),r1
  1554.     move        y:ci1,a
  1555.     move        a1,n1
  1556.     move        #miny,r0
  1557.     lua        (r1)+n1,r7
  1558.     move        #maxy,r4
  1559.     move        #1,b1
  1560.     add        b,a    y:runlink_ptr,r2
  1561.     move        a,n0
  1562.     move        a,n4
  1563. ;-------------------------------------------------------*
  1564. ;    Prefetch loop registers                *
  1565. ;-------------------------------------------------------*
  1566.     move        x:(r0+n0),a
  1567.     move                y:(r4+n4),b
  1568.     move        a,x:(r0)    b,y:(r4)
  1569.     move        a,x:(r3+n3)
  1570.     move                b,y:(r5+n5)
  1571. ;-------------------------------------------------------*
  1572. ;    Calculate span width                *
  1573. ;-------------------------------------------------------*
  1574.     move        y:ci2,b
  1575.     move        y:ci1,a
  1576.     sub        a,b        #edge_increments,r1
  1577.     rts
  1578.  
  1579. ;-------------------------------------------------------*
  1580. ;    Rotate indexes & velocities for floor mapping    *
  1581. ;-------------------------------------------------------*
  1582. RotateMap:
  1583. ;-------------------------------------------------------*
  1584. ;    x1                    ; 24
  1585. ;    y1                    ; 24
  1586. ;    v                    ; 16.8
  1587. ;-------------------------------------------------------*
  1588. ;    tnorm = 320/h                ; 8.16
  1589. ;    xo = (w/2)-x1                ; 24
  1590. ;    uvs = v * tnorm                ; 16.8
  1591. ;    xi = uvs * cos(a)            ; 16.8
  1592. ;    yi = uvs * sin(a)            ; 16.8
  1593. ;    xs = uvs*xo*cos(a) + v*sin(a) - py    ; 16.8
  1594. ;    ys = uvs*xo*sin(a) - v*cos(a) - px    ; 16.8
  1595. ;-------------------------------------------------------*
  1596.     move            y:run_ptr,b
  1597.     move            #runstarts,r6
  1598.     move        r6,a
  1599.     sub        a,b    r6,y:run_ptr
  1600.     asr        b    #HTX,r0
  1601.     jeq        _loop
  1602.     move        #map_data,r7
  1603.     move        #3-1,m7
  1604.     move        #_next,r5
  1605. ;-------------------------------------------------------*
  1606. ;    Read untranslated coordinates from host        *    
  1607. ;-------------------------------------------------------*
  1608.     do        b,_loop
  1609. ;-------------------------------------------------------*
  1610. ;    Fetch backup copies of flat Z & X1        *
  1611. ;-------------------------------------------------------*
  1612. _next:    move        y:(r6)+,x1
  1613.     move        y:(r6)+,a
  1614. ;-------------------------------------------------------*
  1615. ;    xo(24) = (w/2)-x1                *
  1616. ;-------------------------------------------------------*
  1617.     move        a,n0
  1618.     move                y:hwid,x0
  1619.     sub        x0,a        y:lshft7,y0
  1620.     move        a,x0
  1621.     mpy        x0,y0,a        x1,y:map_v
  1622.     move        a0,y1
  1623. ;-------------------------------------------------------*
  1624. ;    uvs(16.8) = v * tnorm                *
  1625. ;-------------------------------------------------------*
  1626.     move                y:map_tnorm,x0
  1627.     mpy        x0,x1,a        y:cosa,x1
  1628.     rep        #(8-1)
  1629.     asl        a
  1630.     move        a,x0
  1631. ;-------------------------------------------------------*
  1632. ;    xi(16.8) = uvs * cos(a)                *
  1633. ;-------------------------------------------------------*
  1634.     mpy        x0,x1,a        x0,y:map_uvs    
  1635.     asl        a        y:lshft18,x1
  1636.     move                a,y:map_cosauvs
  1637.     move        a,y0
  1638.     mpy        y0,x1,a        y:sina,x1
  1639. ;-------------------------------------------------------*
  1640. ;    yi(16.8) = uvs * sin(a)                *
  1641. ;-------------------------------------------------------*
  1642.     mpy        x0,x1,a        a,y:map_xi
  1643.     asl        a        y:lshft18,x1
  1644.     move                a,y:map_sinauvs
  1645.     move        a,x0
  1646.     mpy        -x0,x1,a    y:map_cosauvs,x0
  1647. ;-------------------------------------------------------*
  1648. ;    xs(16.8) = uvs*xo*cos(a) + v*sin(a) - py    *
  1649. ;-------------------------------------------------------*
  1650.     mpy        x0,y1,a        a,y:map_yi    ; 8.24
  1651.     move                y:map_v,x1
  1652.     move                y:sina,x0
  1653.     mpy        x0,x1,b        y:lshft16,x1
  1654.     asl        b
  1655.     move        b,x0
  1656.     mpy        x0,x1,b        y:py,x0
  1657.     add        b,a        y:map_v,x1
  1658.     sub        x0,a        y:map_sinauvs,x0
  1659.     rep        #8+2
  1660.     asl        a
  1661. ;-------------------------------------------------------*
  1662. ;    ys(16.8) = uvs*xo*sin(a) - v*cos(a) - px    *
  1663. ;-------------------------------------------------------*
  1664.     mpy        -x0,y1,a    a,y:map_x
  1665.     move                y:cosa,x0
  1666.     mpy        x0,x1,b        y:lshft16,x1
  1667.     asl        b
  1668.     move        b,x0
  1669.     mac        x0,x1,a        y:px,x0
  1670.     add        x0,a        y:(r7)+,b
  1671.     rep        #8+2
  1672.     asl        a
  1673. ;-------------------------------------------------------*
  1674. ;    Write previously derived coordinates to host    *
  1675. ;-------------------------------------------------------*
  1676.     dspwaitwrite
  1677.     move        b,x:(r0)    y:(r7)+,b
  1678.     dspwaitwrite
  1679.     move        b,x:(r0)    y:(r7)+,b
  1680.     dspwaitwrite
  1681.     move        b,x:(r0)
  1682.     dspwaitwrite
  1683.     move        a,x:(r0)
  1684.     dspwaitwrite
  1685.     move        n0,x:(r0)
  1686. ;-------------------------------------------------------*
  1687. _loop:    move        #-1,m7
  1688.     jmp        command_base
  1689.  
  1690. ;-------------------------------------------------------*
  1691. ;    Calculate & fetch ssector floor & ceiling    *
  1692. ;-------------------------------------------------------*
  1693. GetSsector:
  1694. ;-------------------------------------------------------*
  1695.     move        #<$FF,b
  1696.     move        y:HTX_ptr,r7
  1697.     dspread_s
  1698.     move        a,y:cy
  1699.     dspread_s
  1700.     move        a,y:fy
  1701. ;-------------------------------------------------------*
  1702.     move                y:lshft8,x0
  1703.     move                y:hhig,y0
  1704.     mpy        x0,y0,a        y:vs_width,y1
  1705.     move                y:cy,x1
  1706.     mpy        x0,y1,a        a0,y0
  1707.     move                a0,y1
  1708.     mpy        x1,y1,a        y:fy,x1
  1709.     rep        #(6+2)
  1710.     asr        a        a0,b
  1711.     add        y0,b        a0,a        ; ((w/2) * cy) / cz2 (128)
  1712.     add        y0,a        b,y:cj1        ; cj1
  1713.     mpy        x1,y1,a        a,y:cj2        ; cj2
  1714.     rep        #(6+2)
  1715.     asr        a        a0,b
  1716.     add        y0,b        a0,a        ; ((w/2) * fy) / fz2 (128)
  1717.     add        y0,a        b,y:fj1        ; fj1
  1718.     move                a,y:fj2        ; fj2
  1719. ;-------------------------------------------------------*
  1720.     move                y:lshft8,x0
  1721. ;-------------------------------------------------------*
  1722.     move                y:cj2,b
  1723.     move                y:cj1,x1
  1724.     sub        x1,b        y:ctop,y0        ; (cj2-cj1)
  1725.     mpy        x0,y0,a        #>(1<<8)-1,y0
  1726.     move        a0,a                    ; cj3
  1727.     add        y0,a        y:cj1,y0
  1728.     sub        y0,a        y:lshft15,y0
  1729.     move        a,y1                    ; (cj3-cj1)
  1730.     mpy        -y0,y1,a    b,x1            ; (cz1-cz2)*(cj3-cj1)
  1731.     jsr        divs_x1_a
  1732.     move                #>(128<<8),y1
  1733.     add        y1,a        y:cbot,y0
  1734.     move                a,y:ctop_z
  1735.     mpy        x0,y0,a        #>(1<<8)-1,y0
  1736.     move        a0,a                    ; cj3
  1737.     add        y0,a        y:cj1,y0
  1738.     sub        y0,a        y:lshft15,y0
  1739.     move        a,y1
  1740.     mpy        -y0,y1,a    #>(128<<8),y1
  1741.     jsr        divs_x1_a
  1742.     add        y1,a
  1743.     move                a,y:cbot_z
  1744. ;-------------------------------------------------------*
  1745.     move                y:fj1,b
  1746.     move                y:fj2,x1
  1747.     sub        x1,b        y:ftop,y0        ; (cj2-cj1)
  1748.     mpy        x0,y0,a        #>(1<<8)-1,y0
  1749.     move        a0,a                    ; cj3
  1750.     add        y0,a        y:fj1,y0
  1751.     sub        y0,a        y:lshft15,y0
  1752.     move        a,y1                    ; (cj3-cj1)
  1753.     mpy        y0,y1,a        b,x1            ; (cz1-cz2)*(cj3-cj1)<<8
  1754.     jsr        divs_x1_a
  1755.     move                #>(128<<8),y1
  1756.     add        y1,a        y:fbot,y0
  1757.     move                a,y:ftop_z
  1758.     mpy        x0,y0,a        #>(1<<8)-1,y0
  1759.     move        a0,a                    ; cj3
  1760.     add        y0,a        y:fj1,y0
  1761.     sub        y0,a        y:lshft15,y0
  1762.     move        a,y1
  1763.     mpy        y0,y1,a        #>(128<<8),y1
  1764.     jsr        divs_x1_a
  1765.     add        y1,a
  1766.     move                a,y:fbot_z
  1767. ;-------------------------------------------------------*
  1768. ;    Initialise gradient & send span    to host        *
  1769. ;-------------------------------------------------------*
  1770.     move                y:ctop,a
  1771.     move                y:cbot,b
  1772.     sub        a,b        y:one,a
  1773.     move        b,x0
  1774.     and        #$FE,ccr
  1775.     rep        #24
  1776.     div        x0,a
  1777.     move        a0,x0
  1778.     move                y:ctop_z,a
  1779.     move                y:cbot_z,b
  1780.     sub        a,b        a,l:t
  1781.     move        b,x1
  1782.     mpy        x0,x1,a        #ctop,r0
  1783.     move                a,l:ti
  1784.     move                #cbot,r4
  1785.     move        y:ceiling_luminance,x0
  1786.     jsr        getflatarea
  1787. ;-------------------------------------------------------*
  1788. ;    Initialise gradient & send span    to host        *
  1789. ;-------------------------------------------------------*
  1790.     move                y:ftop,a
  1791.     move                y:fbot,b
  1792.     sub        a,b        y:one,a
  1793.     move        b,x0
  1794.     and        #$FE,ccr
  1795.     rep        #24
  1796.     div        x0,a
  1797.     move        a0,x0
  1798.     move                y:ftop_z,a
  1799.     move                y:fbot_z,b
  1800.     sub        a,b        a,l:t
  1801.     move        b,x1
  1802.     mpy        x0,x1,a        #ftop,r0
  1803.     move                a,l:ti
  1804.     move                #fbot,r4
  1805.     move        y:floor_luminance,x0
  1806.     jsr        getflatarea
  1807. ;-------------------------------------------------------*
  1808.     jmp        NewSsector
  1809.  
  1810. ;-------------------------------------------------------*
  1811. ;    Determine visibility of BSP node        *
  1812. ;-------------------------------------------------------*
  1813. NodeInCone:
  1814. ;-------------------------------------------------------*
  1815.     move        #noderegs,r0
  1816.     move        #<$FF,b
  1817.     move        y:HTX_ptr,r7
  1818.     move        y:py,y0
  1819.     do        #2,_ry
  1820.     dspread_s
  1821.     sub        y0,a
  1822.     move        a,y:(r0)+
  1823. _ry:    move        y:px,x0
  1824.     do        #2,_rx
  1825.     dspread_s
  1826.     sub        x0,a
  1827.     move        a,y:(r0)+
  1828. _rx:
  1829.  
  1830. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1831. ;    Octal segment BSP node eliminator        *
  1832. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1833. ;    Eliminates nodes from 5 out of all 8 octants    *
  1834. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1835. octant_check:
  1836. ;-------------------------------------------------------*
  1837.     move        #project_node,r0
  1838.     move        #oct_x,r1
  1839.     move                y:pangle,x1
  1840.     move                y:rshft13,y1
  1841.     mpy        x1,y1,a        #octs,r7
  1842.     move        a1,n7
  1843.     nop
  1844.     move                y:(r7+n7),r7
  1845.     nop
  1846.     jmp        (r7)
  1847. ;-------------------------------------------------------*
  1848. ;    Octant elimination routines             *
  1849. ;-------------------------------------------------------*
  1850. oct_0:    move                y:node_y1,a
  1851.     tst        a        y:node_x2,b
  1852.     jpl        (r1)
  1853.     cmp        a,b
  1854.     jpl        (r0)
  1855.     jmp        (r1)
  1856. ;-------------------------------------------------------*
  1857. oct_1:    move                y:node_x2,a
  1858.     tst        a        y:node_y1,b
  1859.     jmi        (r1)
  1860.     cmp        b,a
  1861.     jpl        (r0)
  1862.     jmp        (r1)
  1863. ;-------------------------------------------------------*
  1864. oct_2:    move                y:node_x2,a
  1865.     tst        a        y:node_y2,b
  1866.     jmi        (r1)
  1867.     neg        b
  1868.     cmp        b,a
  1869.     jpl        (r0)
  1870.     jmp        (r1)
  1871. ;-------------------------------------------------------*
  1872. oct_3:    move                y:node_y2,a
  1873.     tst        a        y:node_x2,b
  1874.     jmi        (r1)
  1875.     neg        a
  1876.     cmp        a,b
  1877.     jpl        (r0)
  1878.     jmp        (r1)
  1879. ;-------------------------------------------------------*
  1880. oct_4:    move                y:node_y2,a
  1881.     tst        a        y:node_x1,b
  1882.     jmi        (r1)
  1883.     cmp        a,b
  1884.     jmi        (r0)
  1885.     jmp        (r1)
  1886. ;-------------------------------------------------------*
  1887. oct_5:    move                y:node_x1,a
  1888.     tst        a        y:node_y2,b
  1889.     jpl        (r1)
  1890.     cmp        b,a
  1891.     jmi        (r0)
  1892.     jmp        (r1)
  1893. ;-------------------------------------------------------*
  1894. oct_6:    move                y:node_x1,a
  1895.     tst        a        y:node_y1,b
  1896.     jpl        (r1)
  1897.     neg        b
  1898.     cmp        b,a
  1899.     jmi        (r0)
  1900.     jmp        (r1)
  1901. ;-------------------------------------------------------*
  1902. oct_7:    move                y:node_y1,a
  1903.     tst        a        y:node_x1,b
  1904.     jpl        (r1)
  1905.     neg        a
  1906.     cmp        a,b
  1907.     jmi        (r0)
  1908. ;-------------------------------------------------------*
  1909. oct_x:    move        #invisible_code,n0
  1910.     dspwaitwrite
  1911.     move        n0,x:HTX
  1912.     jmp        command_base
  1913.  
  1914. ;-------------------------------------------------------*
  1915. project_node:
  1916. ;-------------------------------------------------------*
  1917.     move        #>$1000,a
  1918.     neg        a        a,y:node_imin
  1919.     move                a,y:node_imax
  1920. ;-------------------------------------------------------*
  1921. ;    Rotate NY & NZ into canonical VZ & VX        *
  1922. ;-------------------------------------------------------*
  1923. ;    x = (dx)*sin(a) + (dy)*cos(a)            *
  1924. ;    z = (dx)*cos(a) - (dy)*sin(a)            *
  1925. ;-------------------------------------------------------*
  1926.     move                y:sina,x1
  1927.     move                y:cosa,y1
  1928.     move                y:node_x1,x0
  1929.     mpy        x0,x1,a        y:node_y1,y0    ; dx*sin(a)
  1930.     mac        y0,y1,a        #vertices,r0    ; + dy*cos(a)
  1931.     mpy        x0,y1,b        #1,n3        ; dx*cos(a)
  1932.     mac        -y0,x1,b    y:node_x2,x0    ; - dy*sin(a)
  1933.     asl        a        y:node_y1,y0
  1934.     asl        b        a,l:(r0)+
  1935.     mpy        x0,x1,a        b,l:(r0)+    ; dx*sin(a)
  1936.     mac        y0,y1,a        #1,n1        ; + dy*cos(a)
  1937.     mpy        x0,y1,b        #1,n2        ; dx*cos(a)
  1938.     mac        -y0,x1,b    y:node_x2,x0    ; - dy*sin(a)
  1939.     asl        a        y:node_y2,y0
  1940.     asl        b        a,l:(r0)+
  1941.     mpy        x0,x1,a        b,l:(r0)+    ; dx*sin(a)
  1942.     mac        y0,y1,a                ; + dy*cos(a)
  1943.     mpy        x0,y1,b                ; dx*cos(a)
  1944.     mac        -y0,x1,b    y:node_x1,x0    ; - dy*sin(a)
  1945.     asl        a        y:node_y2,y0
  1946.     asl        b        a,l:(r0)+
  1947.     mpy        x0,x1,a        b,l:(r0)+    ; dx*sin(a)
  1948.     mac        y0,y1,a                ; + dy*cos(a)
  1949.     mpy        x0,y1,b                ; dx*cos(a)
  1950.     mac        -y0,x1,b            ; - dy*sin(a)
  1951.     asl        a        
  1952.     asl        b        a,l:(r0)+
  1953.     move                b,l:(r0)+    ; dx*sin(a)
  1954. ;-------------------------------------------------------*
  1955. project_line:
  1956. ;-------------------------------------------------------*
  1957.     move        #_swap,r4
  1958.     move        #_abort_line,r5
  1959.     move        #_no_isect,r6
  1960.     move        #linedefs,r7
  1961. ;-------------------------------------------------------*
  1962.     do        #4,_project_loop
  1963. ;-------------------------------------------------------*
  1964.     move        y:(r7)+,r1
  1965.     move        y:(r7),r2
  1966. ;-------------------------------------------------------*
  1967. ;    Reorient line                    *
  1968. ;-------------------------------------------------------*
  1969.     move                l:(r1+n1),a
  1970.     move                l:(r2+n2),b
  1971.     cmp        a,b        r1,n0
  1972.     jpl        (r4)
  1973.     move        r2,r1
  1974.     move        n0,r2
  1975. ;-------------------------------------------------------*
  1976. ;    z-clip line if it intersects with viewplane    *    
  1977. ;-------------------------------------------------------*
  1978. _swap:    move                y:viewplane,a
  1979.     move                l:(r2+n2),b
  1980.     cmp        b,a        l:(r1+n1),x
  1981.     jpl        (r5)
  1982. ;-------------------------------------------------------*
  1983.     move                r1,r3
  1984.     sub        x,a        l:(r1),x    ; (cz-z1)
  1985.     jle        (r6)
  1986. ;-------------------------------------------------------*
  1987.     move                #temp1,r3
  1988.     move                l:(r2),b
  1989.     sub        x,b        a1,x0        ; (cz-z1)
  1990.     move                b1,y0        ; (x2-x1)
  1991.     mpy        x0,y0,a        l:(r2+n2),b    ; (x2-x1)*(cz-z1)
  1992.     move                l:(r1+n1),y
  1993.     sub        y,b        a,y:0
  1994.     abs        a        b1,x0        ; (z2-z1)
  1995.     and        #$FE,ccr
  1996.     rep        #24
  1997.     div        x0,a                ; ((x2-x1)*(cz-z1))/(z2-z1)
  1998.     move        a0,a
  1999.     jclr        #23,y:0,_nneg
  2000.     neg        a
  2001. ;-------------------------------------------------------*
  2002. _nneg:    move                l:(r1),b
  2003.     add        a,b        y:viewplane,a
  2004.     move        b,x:(r3)            ; x1' = (((x2-x1)*(cz-z1))/(z2-z1))+x1
  2005.     move        a,x:(r3+n3)            ; z1' = cz
  2006. ;-------------------------------------------------------*
  2007. _no_isect:
  2008. ;-------------------------------------------------------*
  2009. ;    Project points into 2D viewspace        *
  2010. ;-------------------------------------------------------*
  2011.     move                y:hs_width,x0
  2012.     move                x:(r3),x1    ; x1
  2013.     move                x:(r2),y1    ; x2
  2014.     mpy        x0,x1,a        x:(r3+n3),x1    ; z1
  2015.     mpy        x0,y1,b        x:(r2+n2),y1    ; z2
  2016.     move                a0,x0        ; (x1*hw)*2
  2017.     move                y:one,a
  2018.     tfr        a,b        b0,y0        ; (x2*hw)*2
  2019.     and        #$FE,ccr
  2020.     rep        #24
  2021.     div        x1,a
  2022.     move                a0,x1        ; 1/(z1*2)
  2023.     and        #$FE,ccr
  2024.     rep        #24
  2025.     div        y1,b
  2026.     mpy        -x0,x1,a    b0,y1        ; 1/(z2*2)
  2027.     asr        a
  2028.     mpy        -y0,y1,b    a1,x0        ; (x1*hw) / z1 
  2029.     asr        b        y:node_imin,a
  2030.     move                b1,y0        ; (x2*hw) / z2
  2031. ;-------------------------------------------------------*
  2032. ;    Adjust bounds                    *
  2033. ;-------------------------------------------------------*
  2034.     cmp        x0,a        y:node_imax,b
  2035.     tgt        x0,a
  2036.     cmp        y0,a
  2037.     tgt        y0,a
  2038.     cmp        x0,b        a,y:node_imin
  2039.     tmi        x0,b
  2040.     cmp        y0,b
  2041.     tmi        y0,b
  2042.     move                b,y:node_imax
  2043. ;-------------------------------------------------------*
  2044. _abort_line:
  2045. ;-------------------------------------------------------*
  2046.     move        #0,y0
  2047. ;-------------------------------------------------------*
  2048. _project_loop:
  2049. ;-------------------------------------------------------*
  2050. ;    Inside / outside viewport check            *
  2051. ;-------------------------------------------------------*
  2052.     move                y:one,y1
  2053.     move                y:hwid,x0
  2054.     move                y:node_imax,b
  2055.     add        y1,b        #_end,r1
  2056.     add        x0,b        y:node_imin,a
  2057.     jmi        (r1)
  2058.     sub        y1,a        #_loop,r3
  2059.     add        x0,a        y:width,x1
  2060.     tmi        y0,a
  2061.     cmp        x1,a        #occlusion_list,r2
  2062.     jpl        (r1)
  2063.     cmp        x1,b        a,n2
  2064.     tpl        x1,b
  2065.     sub        a,b        (r2)+n2
  2066.     jle        (r1)
  2067. ;-------------------------------------------------------*
  2068. ;    Occlusion check                    *
  2069. ;-------------------------------------------------------*
  2070.     move        y:one,a
  2071. _loop:    jset        #0,x:(r2)+,_out
  2072.     sub        a,b
  2073.     jne        (r3)
  2074. ;-------------------------------------------------------*
  2075. _end:    move        #invisible_code,n1
  2076. _out:    dspwaitwrite
  2077.     move        n1,x:HTX
  2078.     jmp        command_base
  2079.  
  2080. ;-------------------------------------------------------*
  2081. ProjectWall:
  2082. ;-------------------------------------------------------*
  2083.     move        y:HTX_ptr,r7
  2084.     move        #<$FF,b
  2085.     move        #line_regs,r0
  2086.     move        y:lshft8,y1
  2087.     move        y:px,x0
  2088.     move        y:py,y0
  2089. ;-------------------------------------------------------*
  2090. ;    Read line points from host (16.8)        *
  2091. ;-------------------------------------------------------*
  2092.     do        #2,_line
  2093.     dspwaitread
  2094.     move            x:(r7),n7
  2095.     move        n7,a
  2096.     jclr        #15,n7,_ns1
  2097.     add        b,a
  2098. _ns1:    dspwaitread
  2099.     sub        x0,a    x:(r7),n7
  2100.     move        a1,x1
  2101.     mpy        x1,y1,a
  2102.     move        a0,x:(r0)+
  2103.     move        n7,a
  2104.     jclr        #15,n7,_ns2
  2105.     add        b,a
  2106. _ns2:    sub        y0,a
  2107.     move        a1,x1
  2108.     mpy        x1,y1,a
  2109.     move        a0,x:(r0)+
  2110. ;-------------------------------------------------------*
  2111. ;    Rotate NY & NZ into canonical VZ & VX        *
  2112. ;-------------------------------------------------------*
  2113. ;    x(16.8) = (dx)*sin(a) + (dy)*cos(a)        *
  2114. ;    z(16.8) = (dx)*cos(a) - (dy)*sin(a)        *
  2115. ;-------------------------------------------------------*
  2116. _line:    move        #line_regs,r0
  2117.     move                y:sina,x1
  2118.     move                y:cosa,y1
  2119.     move                x:(r0)+,x0
  2120.     mpy        x0,x1,a        x:(r0)+,y0    ; dx*sin(a)
  2121.     mac        y0,y1,a        #2,r1        ; + dy*cos(a)
  2122.     mpy        x0,y1,b        x:(r0)+,x0    ; dx*cos(a)
  2123.     mac        -y0,x1,b    x:(r0)+,y0    ; - dy*sin(a)
  2124.     asl        a        #1,n1
  2125.     asl        b        a,x:(r1)
  2126.     mpy        x0,x1,a        b,x:(r1+n1)    ; dx*sin(a)
  2127.     mac        y0,y1,a        b,x:8        ; + dy*cos(a)
  2128.     mpy        x0,y1,b        #4,r2        ; dx*cos(a)
  2129.     mac        -y0,x1,b    #1,n2        ; - dy*sin(a)
  2130.     asl        a
  2131.     asl        b        a,x:(r2)
  2132.     move                b,x:(r2+n2)
  2133. ;-------------------------------------------------------*
  2134. ;    Check z-direction of line            *
  2135. ;-------------------------------------------------------*
  2136.     move                x:(r1+n1),a    ; z1
  2137.     cmp        a,b        b,x:9        ; z1 <=> z2 ?
  2138.     jpl        _posi
  2139. ;-------------------------------------------------------*
  2140. ;    Reorient line for z-clip test            *
  2141. ;-------------------------------------------------------*
  2142.     move        #4,r1    
  2143.     move        #2,r2    
  2144. ;-------------------------------------------------------*
  2145. ;    Discard line if it lies behind viewer        *
  2146. ;-------------------------------------------------------*
  2147. _posi:    move                #>(12<<8),a    ; zc
  2148.     move                x:(r2+n2),b    ; z2
  2149.     cmp        b,a        x:(r1+n1),x0    ; z2 <=> zc ?
  2150.     jpl        _end
  2151. ;-------------------------------------------------------*
  2152. ;    Ignore line if it lies in front of viewer    *
  2153. ;-------------------------------------------------------*
  2154.     sub        x0,a        x:(r1),x0    ; (cz-z1)
  2155.     jle        _no_isect
  2156. ;-------------------------------------------------------*
  2157. ;    Line intersects with viewplane             *
  2158. ;-------------------------------------------------------*
  2159.     move                x:(r2),b    ; x2
  2160.     sub        x0,b        a1,x0        ; (cz-z1)
  2161.     move                b1,y0        ; (x2-x1)
  2162.     mpy        x0,y0,a        x:(r2+n2),b    ; (x2-x1)*(cz-z1)
  2163.     move                x:(r1+n1),y0
  2164.     sub        y0,b        a,y:0
  2165.     abs        a        b1,x0        ; (z2-z1)
  2166.     and        #$FE,ccr
  2167.     rep        #24
  2168.     div        x0,a                ; ((x2-x1)*(cz-z1))/(z2-z1)
  2169.     move        a0,a
  2170.     jclr        #23,y:0,_xp
  2171.     neg        a
  2172. ;-------------------------------------------------------*
  2173. ;    Replace p1' with new intersection point        *
  2174. ;-------------------------------------------------------*
  2175. _xp:    move                x:(r1),b
  2176.     add        a,b        #>(12<<8),a
  2177.     move                b,x:(r1)    ; x1' = (((x2-x1)*(cz-z1))/(z2-z1))+x1
  2178.     move                a,x:(r1+n1)    ; z1' = cz
  2179. ;-------------------------------------------------------*
  2180. _no_isect:
  2181. ;-------------------------------------------------------*
  2182. ;    Reorient line back to original configuration    *
  2183. ;-------------------------------------------------------*
  2184.     move        #2,r1    
  2185.     move        #4,r2    
  2186. ;-------------------------------------------------------*
  2187. ;    Project points into 2D viewspace        *
  2188. ;-------------------------------------------------------*
  2189.     move                y:hs_width,x0
  2190.     move                y:lshft8,x1
  2191.     mpy        -x0,x1,a    x:(r1),x1    ; x1
  2192.     move        a0,x0
  2193.     move                x:(r2),y1    ; x2
  2194.     mpy        x0,x1,a        x:(r1+n1),x1    ; z1
  2195.     mpy        x0,y1,b        x:(r2+n2),y1    ; z2
  2196.     abs        a        a,x:0
  2197.     abs        b        b,y:0
  2198.     and        #$FE,ccr
  2199.     rep        #24
  2200.     div        x1,a
  2201.     move        a0,a                ; (x1*hw)/z1
  2202.     and        #$FE,ccr
  2203.     rep        #24
  2204.     div        y1,b
  2205.     move        b0,b                ; (x2*hw)/z2
  2206.     jclr        #23,x:0,_x1p
  2207.     neg        a
  2208. _x1p:    move        a,x1
  2209.     jclr        #23,y:0,_x2p
  2210.     neg        b
  2211. _x2p:    move        b,y1
  2212. ;-------------------------------------------------------*
  2213. ;    Inside / outside viewport check            *
  2214. ;-------------------------------------------------------*
  2215.     move                y:rshft8,x0
  2216.     mpy        x0,x1,a        a,x:(r1)
  2217.     mpy        x0,y1,b        b,x:(r2)
  2218.     move                y:hwid,x0
  2219.     move        a1,a
  2220.     move        b1,b
  2221.     add        x0,b        #0,y0
  2222.     jmi        _end
  2223.     add        x0,a        y:width,x1
  2224.     tmi        y0,a
  2225.     cmp        x1,a        #occlusion_list,r4
  2226.     jpl        _end
  2227.     cmp        x1,b        a1,n4
  2228.     tpl        x1,b
  2229.     sub        a,b        (r4)+n4
  2230.     jle        _end
  2231. ;-------------------------------------------------------*
  2232. ;    Occlusion check                    *
  2233. ;-------------------------------------------------------*
  2234.     move        #_loop,r0
  2235.     move        y:one,a
  2236. _loop:    jset        #0,x:(r4)+,_out
  2237.     sub        a,b
  2238.     jne        (r0)
  2239. ;-------------------------------------------------------*
  2240. _end:    move        #0,n4
  2241.     dspwaitwrite
  2242.     move        n4,x:(r7)
  2243.     jmp        command_base
  2244. ;-------------------------------------------------------*
  2245. ;    Return (x1,z1)/(x2,z2)/(rz1,rz2) data to host    *
  2246. ;-------------------------------------------------------*
  2247. _out:    dspwaitwrite
  2248.     move        a,x:(r7)
  2249.     move        y:hwid_8,b
  2250.     move                x:(r1),a
  2251.     add        b,a        x:(r1+n1),x0
  2252.     dspwaitwrite
  2253.     move        a,x:(r7)
  2254.     dspwaitwrite
  2255.     move        x0,x:(r7)
  2256.     move                x:(r2),a
  2257.     add        b,a        x:(r2+n2),x0
  2258.     dspwaitwrite
  2259.     move        a,x:(r7)
  2260.     dspwaitwrite
  2261.     move        x0,x:(r7)
  2262.     move        x:8,a
  2263.     dspwaitwrite
  2264.     move        a,x:(r7)
  2265.     move        x:9,a
  2266.     dspwaitwrite
  2267.     move        a,x:(r7)
  2268.     jmp        command_base
  2269.  
  2270. ;-------------------------------------------------------*
  2271. ;    Initialise structures for a new ssector        *
  2272. ;-------------------------------------------------------*
  2273. NewSsector:
  2274. ;-------------------------------------------------------*
  2275.     move        y:runlink_ptr,r2
  2276.     move        y:two,n2
  2277.     move        y:height,x1
  2278.     tfr        x1,a        #0,x0
  2279.     asl        a        y:lastslots_ptr,r6
  2280.     move                a,y:index
  2281.     clr        b        n2,a1
  2282.     rep        x1
  2283.     add        a,b    x0,x:(r2)+n2    b,y:(r6)+
  2284. _vert:    move        #0,b1
  2285.     move        b1,y:cbot
  2286.     move        b1,y:fbot
  2287.     move        #max_lines,b1
  2288.     move        b1,y:ctop
  2289.     move        b1,y:ftop
  2290.     jmp        command_base
  2291.  
  2292. ;-------------------------------------------------------*
  2293. ;    Initialise structures for a new scene        *
  2294. ;-------------------------------------------------------*
  2295. NewScene:
  2296. ;-------------------------------------------------------*
  2297.     move        #<$FF,b
  2298.     move        y:HTX_ptr,r7
  2299.     dspread_s    
  2300.     move        a,y:px
  2301.     dspread_s    
  2302.     move        a,y:py
  2303.     dspread_u    a
  2304.     move        a,y:pangle
  2305.     dspread        y:sina
  2306.     dspread        y:cosa
  2307.     move        #occlusion_list,r7
  2308.     move        #(miny+1),r0
  2309.     move        #(maxy+1),r4
  2310.     move        #1,a0
  2311.     move        #0,y0
  2312.     move        y:height,y1
  2313.     do        y:width,_clr
  2314.     move        a0,x:(r7)+
  2315.     move        y0,x:(r0)+
  2316.     move        y1,y:(r4)+
  2317. _clr:    move        #runstarts,r5
  2318.     move        r5,y:run_ptr
  2319.     jmp        command_base
  2320.  
  2321. ;-------------------------------------------------------*
  2322. ;    Resize viewport                    *
  2323. ;-------------------------------------------------------*
  2324. ResizeWindow:
  2325. ;-------------------------------------------------------*
  2326.     move        y:HTX_ptr,r7
  2327.     move        #window_regs,r0
  2328.     do        #2,_lp1
  2329.     dspread_u    a
  2330.     asr        a        a,y:(r0)+
  2331.     move                a,y:(r0)+
  2332. _lp1:    do        #3,_lp2
  2333.     dspread_u    a
  2334.     move                a,y:(r0)+
  2335. _lp2:    move        y:hwid,x0
  2336.     move        y:lshft8,x1
  2337.     mpy        x0,x1,a
  2338.     move        a0,y:hwid_8
  2339. ;-------------------------------------------------------*
  2340.     move        y:x1list_ptr,r0
  2341.     move        y:x2list_ptr,r4
  2342.     clr        b        y:width,a
  2343.     move        #2,b1
  2344.     add        b,a
  2345.     do        a1,_clr
  2346.     move        a0,x:(r0)+
  2347.     move        a0,x:(r4)+
  2348. ;-------------------------------------------------------*
  2349. ;    tnorm(8.16) = ((320/hwid)*vs_width)/r_height    *
  2350. ;-------------------------------------------------------*
  2351. _clr:    move        y:r_height,x0
  2352.     move        #>320,a
  2353.     rep        #(8-1)
  2354.     asr        a
  2355.     and        #$FE,ccr
  2356.     rep        #24
  2357.     div        x0,a
  2358.     move        a0,x0
  2359.     move                y:vs_width,x1
  2360.     mpy        x0,x1,a        y:hwid,x1
  2361.     and        #$FE,ccr
  2362.     rep        #24
  2363.     div        x1,a
  2364.     move        a0,y:map_tnorm
  2365.     jmp        command_base
  2366.  
  2367. ;-------------------------------------------------------*
  2368. ;    Select projected curve interpolation        *
  2369. ;-------------------------------------------------------*
  2370. PerspectCalc:
  2371. ;-------------------------------------------------------*
  2372.     move        #perspected_column,r0
  2373.     move        r0,y:column_rout
  2374.     jmp        command_base
  2375.  
  2376. ;-------------------------------------------------------*
  2377. ;    Select (faster) linear interpolation        *
  2378. ;-------------------------------------------------------*
  2379. LinearCalc:
  2380. ;-------------------------------------------------------*
  2381.     move        #linear_column,r0
  2382.     move        r0,y:column_rout
  2383.     jmp        command_base
  2384.  
  2385. ;-------------------------------------------------------*
  2386. ;    Set new ssector luminance            *
  2387. ;-------------------------------------------------------*
  2388. SetLuminance:
  2389. ;-------------------------------------------------------*
  2390.     move        y:HTX_ptr,r7
  2391.     dspread_u    x0
  2392.     dspread_u    y:ceiling_luminance
  2393.     dspread_u    y:floor_luminance
  2394.     jsr        new_luminance
  2395.     jmp        command_base
  2396.  
  2397. ;-------------------------------------------------------*
  2398. new_luminance:
  2399. ;-------------------------------------------------------*
  2400.     move        #>255,a
  2401.     sub        x0,a
  2402.     move        a,x1
  2403.     mpy        x0,x1,b
  2404.     rep        #3+2
  2405.     asr        b
  2406.     asr        a    b0,y:mlum2
  2407.     asr        a
  2408.     neg        a
  2409.     add        x0,a
  2410.     asr        a
  2411.     asr        a
  2412.     move        a,y:mlum3
  2413.     rts
  2414.  
  2415. ;-------------------------------------------------------*
  2416. ;    Generic divide routine                *
  2417. ;-------------------------------------------------------*
  2418. divs_x1_a:
  2419. ;-------------------------------------------------------*
  2420.     abs        a        a,y:0
  2421.     and        #$FE,ccr
  2422.     rep        #24
  2423.     div        x1,a
  2424.     move        a0,a
  2425.     jclr        #23,y:0,_nn
  2426.     neg        a
  2427. _nn:    rts
  2428.  
  2429. ;-------------------------------------------------------*
  2430. ;    Read real number (24.24) from host (16.16)    *
  2431. ;-------------------------------------------------------*
  2432. read_real:
  2433. ;-------------------------------------------------------*
  2434. _rd1    jclr        #0,x:<<HSR,_rd1
  2435.     move        x:(r7),n7
  2436.     move        n7,x1
  2437.     mpy        x1,x0,a
  2438.     move        a0,y:(r6)
  2439. _rd2    jclr        #0,x:<<HSR,_rd2
  2440.     move        x:(r7),n7
  2441.     move        n7,a
  2442.     jclr        #15,n7,_sk
  2443.     add        b,a
  2444. _sk:    move        a1,x:(r6)
  2445.     rts
  2446.  
  2447. ;-------------------------------------------------------*
  2448. start:
  2449. ;-------------------------------------------------------*
  2450.     bclr        #2,omr
  2451. ;-------------------------------------------------------*
  2452. command_base:
  2453. ;-------------------------------------------------------*
  2454. ;    Execute next command                *
  2455. ;-------------------------------------------------------*
  2456.     clr        b    #commands,r7
  2457.     dspwaitread    
  2458.     move        x:HTX,a2
  2459.     move        a2,n7
  2460.     nop
  2461.     move        y:(r7+n7),r7
  2462.     nop
  2463.     jmp        (r7)
  2464.  
  2465. ;-------------------------------------------------------*
  2466. ;    Mark end of external program space        *
  2467. ;-------------------------------------------------------*
  2468.  
  2469. endcode:
  2470. lasty        =    endcode
  2471.  
  2472. ;-------------------------------------------------------*
  2473.     org        y:lasty
  2474. ;-------------------------------------------------------*
  2475. ;    External Y-buffers                *
  2476. ;-------------------------------------------------------*
  2477.  
  2478. lastslots:        ds    max_lines        ; floating slot pointers for last-links
  2479.  
  2480. runstarts:                        ; run cache
  2481.  
  2482. ;-------------------------------------------------------*
  2483.